I have this code:
var str = ' abc';
str.replace(" ","");
b开发者_如何学Pythonut it is not working.
First, you have spelling error:
replce
You should do:
var str = ' abc';
str = str.replace(/ /g,"");
The /g
is global modifier which will replace the specified text throughout the given string.
Alternatively, you can use split
and join
like this:
var str = ' abc';
str = str.split(" ").join("");
var s='anishmarokey';
s.replace('a','b');
try this
var str = ' abc';
str=str.replace(/ /g,"");
Try this in any browser url
javascript:alert('strin g'.replace(/ /g, ''));
You need to use regex, not plain strings for js string.replace
var str = ' abc'; str = str.replace(/ /g, '');
Split the string by spaces and get the second part of the split string.
var newstr = str.split(" ");
str = newstr[1];
精彩评论