How to remove empty spaces in the url - javascript

Hi how can i remove empty space in the URL using javascript:
here how it looks like
stockcode=1ECN0010-000&quantity=100&wiretype=SAVSS0.85B&wirelength=0.455&terminalA=916189-000%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20&sealA=255146-000%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20&terminalB=916876-010&sealB=255146-000
This is what i want:
stockcode=1ECN0010-000&quantity=100&wiretype=SAVSS0.85B&wirelength=0.455&terminalA=916189-000&sealA=255146-000&terminalB=916876-010&sealB=255146-000

Just decode the url, use the replace function to eliminate the whitespaces and then encode it again.
function removeSpaces(url) {
return encodeURIComponent(decodeURIComponent(url).replace(/\s+/g, ''));
}
JS replace-Function to eliminate whitespaces: How to remove spaces from a string using JavaScript?
Javascript decode and encode URI: http://www.w3schools.com/jsref/jsref_decodeuri.asp

If theString has your original string, try:
theString = theString.replace(/%20/g, "")

you can simply replace the substring you want ("%20") with nothing (or "").
Try something like this:
var str = "stockcode=1ECN0010-000&quantity=100&wiretype=SAVSS0.85B&wirelength=0.455&terminalA=916189-000%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20&sealA=255146-000%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20&terminalB=916876-010&sealB=255146-000"
var res = str.replace(/%20/g, "");
Hope it helps.
The idea is just the 1%, now go make the 99% !

try this function :
function Replace(str) {
var del = new RegExp('%20');
str.match(del);
}

This Might Help you:
function doIt(){
var url="stockcode=1ECN0010-000&quantity=100&wiretype=SAVSS0.85B&wirelength=0.455&terminalA=916189-000%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20&sealA=255146-000%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20&terminalB=916876-010&sealB=255146-000";
var str=url.toString();
str=str.split("%20").join("");
return str;
}
Here is Working Fiddle.

You will need to treat the URL as string and remove the whitespace inside that string using your programming language.
For example, if you use JavaScript to remove the whitespace from the URL you can do this:
let string = `https://example.com/ ?key=value`;
string.replace(/\s+/g, "");
This will give you: https://example.com/?key=value;

Related

Javascript: split a string according to two criteria?

I am trying to count the number of sentences in a paragraph. In the paragraph, all sentences end with either ''.'' or ''!''.
My idea is to first split the paragraph into strings whenever there's a ''.'' or ''!'' and then count the number of splitted strings.
I have tried
.split('.' || '!')
but that does not work. It only splits strings whenever there is a ''.''
May I know how to deal with this?
Just use a Regexp, it's pretty simple ;)
const example = 'Hello! You should probably use a regexp. Nice isn\'t it?';
console.log(example.split(/[.!]/));
You will need to use a regex for this.
The following should work:
.split(/\.|!/)
You can use regex /\.|!/ in split() as str.split(/\.|!/) :
var str = 'some.string';
console.log(str.split(/\.|!/));
str = 'some.string!name';
console.log(str.split(/\.|!/));
const sampleString = 'I am handsome. Are you sure?! Just kidding. Thank you.';
const result = sampleString.split(/\.|!/)
console.log(result);
// to remove elements that has no value you can do
const noEmptyElements = result.filter(str => str);
console.log(noEmptyElements);
Try below code it will give you an exact count of sentences in the paragraph.
function count(string,char) {
var re = new RegExp(char,"gi");
return string.match(re).length;
}
function myFunction() {
var str = 'but that! does! not work. It only splits strings whenever there is a. ';
console.log(count(str,'[.?!]'));
}

Replace in javascript

I have a string something like this:
http://stackoverflow.com/questions/ask
And would like to return this part:
http://stackoverflow.com/questions/
How can I do this using pure javascript?
Thanks!
This will match and remove the last part of a string after the slash.
url = "http://stackoverflow.com/questions/ask"
base = url.replace(/[^/]*$/, "")
document.write(base)
Help from: http://www.regexr.com/
For slicing off last part:
var test = 'http://stackoverflow.com/questions/ask';
var last = test.lastIndexOf('/');
var result = test.substr(0, last+1);
document.write(result);
You can accomplish this with the .replace() method on String objects.
For example:
//Regex way
var x = "http://stackoverflow.com/questions/ask";
x = x.replace(/ask/, "");
//String way
x = x.replace('ask', "");
//x is now equal to the string "http://stackoverflow.com/questions/"
The replace method takes two parameters. The first is what to replace, which can either be a string or regex, literal or variable, and the second parameter is what to replace it with.

Removing spaces, hyphen and brackets from a string

I am getting a string like:
var str = '+91 1234567891,(432)123234,123-123-13456,(432)(567)(1234)';
I want to remove the spaces, hyphen and brackets from every number. Something like:
var str = '+911234567891,432123234,12312313456,4325671234';
Please suggest a way to achieve this.
This will do your job:
var str = '+91 1234567891,(432)123234,123-123-13456,(432)(567)(1234)';
var result = str.replace(/[- )(]/g,'');
alert(result);
You can use Regular Expression to replace those items by empty string:
'+91 1234567891,(432)123234,123-123-13456,(432)(567)(1234)'.replace(/[\s()-]+/gi, '');
// results in "+911234567891,432123234,12312313456,4325671234"
Hope it helps.

Regex/Replace any chars inside match combination

For example
var string = 'width="300" height="650"'
i would like to update height in that string and get something like
string = string.replace(/height="..."/g, 'height="150"');
//... as any symbol
how to make reg expression who does not care value of height, to replace it with new one?
You can play with it here, in example:
JsFiddle
var string = 'width="300" height="650"';
string = string.replace(new RegExp(/height=\"[0-9]+\"/g), 'height="150"');
DEMO
for case insensitive use:
.replace(new RegExp(/height=\"[0-9]+\"/gi), 'height="150"');

Remove everything after a certain character

Is there a way to remove everything after a certain character or just choose everything up to that character? I'm getting the value from an href and up to the "?", and it's always going to be a different amount of characters.
Like this
/Controller/Action?id=11112&value=4444
I want the href to be /Controller/Action only, so I want to remove everything after the "?".
I'm using this now:
$('.Delete').click(function (e) {
e.preventDefault();
var id = $(this).parents('tr:first').attr('id');
var url = $(this).attr('href');
console.log(url);
}
You can also use the split() function. This seems to be the easiest one that comes to my mind :).
url.split('?')[0]
jsFiddle Demo
One advantage is this method will work even if there is no ? in the string - it will return the whole string.
var s = '/Controller/Action?id=11112&value=4444';
s = s.substring(0, s.indexOf('?'));
document.write(s);
Sample here
I should also mention that native string functions are much faster than regular expressions, which should only really be used when necessary (this isn't one of those cases).
Updated code to account for no '?':
var s = '/Controller/Action';
var n = s.indexOf('?');
s = s.substring(0, n != -1 ? n : s.length);
document.write(s);
Sample here
var href = "/Controller/Action?id=11112&value=4444";
href = href.replace(/\?.*/,'');
href ; //# => /Controller/Action
This will work if it finds a '?' and if it doesn't
May be very late party :p
You can use a back reference $'
$' - Inserts the portion of the string that follows the matched substring.
let str = "/Controller/Action?id=11112&value=4444"
let output = str.replace(/\?.*/g,"$'")
console.log(output)
It works for me very nicely:
var x = '/Controller/Action?id=11112&value=4444';
var remove_after= x.indexOf('?');
var result = x.substring(0, remove_after);
alert(result);
If you also want to keep "?" and just remove everything after that particular character, you can do:
var str = "/Controller/Action?id=11112&value=4444",
stripped = str.substring(0, str.indexOf('?') + '?'.length);
// output: /Controller/Action?
You can also use the split() method which, to me, is the easiest method for achieving this goal.
For example:
let dummyString ="Hello Javascript: This is dummy string"
dummyString = dummyString.split(':')[0]
console.log(dummyString)
// Returns "Hello Javascript"
Source: https://thispointer.com/javascript-remove-everything-after-a-certain-character/
if you add some json syringified objects, then you need to trim the spaces too... so i add the trim() too.
let x = "/Controller/Action?id=11112&value=4444";
let result = x.trim().substring(0, x.trim().indexOf('?'));
Worked for me:
var first = regexLabelOut.replace(/,.*/g, "");
It can easly be done using JavaScript for reference see link
JS String
EDIT
it can easly done as. ;)
var url="/Controller/Action?id=11112&value=4444 ";
var parameter_Start_index=url.indexOf('?');
var action_URL = url.substring(0, parameter_Start_index);
alert('action_URL : '+action_URL);

Categories