You have to tell replace() to repeat the regex:
(你必须告诉replace()重复正则表达式:)
.replace(/ /g,'')
The g character means to repeat the search through the entire string.
(g字符表示在整个字符串中重复搜索。)
Read about this, and other RegEx modifiers available in JavaScript here . (在此处阅读此处以及JavaScript中提供的其他RegEx修饰符。)
If you want to match all whitespace, and not just the literal space character, use \s
instead:
(如果要匹配所有空格,而不仅仅是文字空格字符,请使用\s
代替:)
.replace(/s/g,'')
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…