如果我有一个单词之间有多个空格的字符串:
Be an excellent person
Run Code Online (Sandbox Code Playgroud)
使用JavaScript/regex,如何删除无关的内部空间,使其变为:
Be an excellent person
Run Code Online (Sandbox Code Playgroud)
dhe*_*aur 19
你可以使用正则表达式/\s{2,}/g:
var s = "Be an excellent person"
s.replace(/\s{2,}/g, ' ');
Run Code Online (Sandbox Code Playgroud)
这个正则表达式应该解决这个问题:
var t = 'Be an excellent person';
t.replace(/ {2,}/g, ' ');
// Output: "Be an excellent person"
Run Code Online (Sandbox Code Playgroud)
像这样的东西应该能够做到.
var text = 'Be an excellent person';
alert(text.replace(/\s\s+/g, ' '));
Run Code Online (Sandbox Code Playgroud)