javascript 使用javascript删除字符串之间的空格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18159216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove white space between the string using javascript
提问by user2660267
I have string. I just want to remove all white spaces between all characters.Please reply "PB 10 CV 2662" to "PB10CV2662"
我有字符串。我只想删除所有字符之间的所有空格。请回复“PB 10 CV 2662”到“PB10CV2662”
回答by jh314
This should do the trick:
这应该可以解决问题:
var str = "PB 10 CV 2662";
str = str.replace(/ +/g, "");
回答by Answer
Try this:
试试这个:
var s = "PB 10 CV 2662";
s.replace(/\s+/g, '');
OR
或者
s.replace(/\s/g, '');
回答by IonicBurger
var str = "PB 10 CV 2662";
str = str.split(" ") //[ 'PB', '10', 'CV', '2662' ]
str = str.join("") // 'PB10CV2662'
OR in one line:
str = str.split(" ").join("")
回答by geekchic
var str = "PB 10 CV 2662";
var cleaned = str.replace(/\s+/g, "");
回答by talemyn
The easiest way would be to use the replace()
method for strings:
最简单的方法是使用replace()
字符串的方法:
var stringVal = "PB 10 CV 2662";
var newStringVal = stringVal.replace(/ /g, "");
That will take the current string value and create a new one where all of the spaces are replaced by empty strings.
这将采用当前字符串值并创建一个新值,其中所有空格都被空字符串替换。
回答by Bryan
Try:
尝试:
var sample_str = "PB 10 CV 2662"
var new_str = sample_str.split(" ").join("")
Or you could use .replace
with the global flag like so:
或者你可以.replace
像这样使用全局标志:
var sample_str = "PB 10 CV 2662"
var new_str = sample_str.replace(" ","","g")
Both will result in new_str being equal to "PB10CV2662". Hope this is of use to you.
两者都会导致 new_str 等于“PB10CV2662”。希望这对你有用。
回答by Sekhar Boyina
value = value.replace(/(\r\n\s|\n|\r|\s)/gm, '');