[英]javascript string remove white spaces and hyphens
I would like to remove white spaces and hyphens from a given string. 我想从给定的字符串中删除空格和连字符。
var string = "john-doe alejnadro";
var new_string = string.replace(/-\s/g,"")
Doesn't work, but this next line works for me: 不起作用,但下一行对我有用:
var new_string = string.replace(/-/g,"").replace(/ /g, "")
How do I do it in one go ? 我该如何一次性完成?
Use alternation : 使用交替 :
var new_string = string.replace(/-|\s/g,"");
a|b
will match either a
or b
, so this matches both hyphens and whitespace. a|b
将匹配 a
或b
,所以这两个连字符和空格相匹配。
Example: 例:
> "hyphen-containing string".replace(/-|\s/g,"")
'hyphencontainingstring'
You have to use: 你必须使用:
var new_string = string.replace(/[-\s]/g,"")
/-\\s/
means hyphen followed by white space. /-\\s/
表示连字符后跟空格。
Use This for Hyphens 用于连字符
var str="185-51-671";
var newStr = str.replace(/-/g, "");
White Space 白色空间
var Actuly = newStr.trim();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.