试图在不删除非字母数字字符的情况下去除其周围的任何空格,例如/\s*(\W)\s*/g
默认会去掉。
有没有办法只使用.replace()来定位空格?
例如:
var regex = "/\s*(\W)\s*/g";
var text = "Words : \" text docs\"";
var text = text.replace(regex , "");
console.log(text)
期望的文本:"Words:"text docs"
类似于Python中去除特殊字符和单词之间的空格,但需用JavaScript实现。
[^\w ]
而不是\W
,以避免匹配空格。) - Enlicotext.replace(/\s*(\W)\s*/g , "$1")
?? - Ritesh Khandekar