开发者

How can I replace space with another string in JavaScript?

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];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜