开发者

How can I replace space with another string in JavaScript?

开发者 https://www.devze.com 2023-02-11 15:46 出处:网络
I have this code: var str = \'abc\'; str.replace(\" \",\"\"); b开发者_如何学Pythonut it is not working.First, you have spelling error:

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

精彩评论

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