标签: javascript
例如
1."abc"; ===>abc 2."ab c"; ===>ab_c 3."ab c"; ===>ab_c 4."ab c" ===>ab_c
同样适用于多个连续的空格 我怎样才能实现这个目标?
答案 0 :(得分:1)
您可以将 replace() 与正则表达式 \s+
replace()
\s+
function removeSpace(str) { return str.replace(/\s+/g, '_'); } console.log(removeSpace('abc')); console.log(removeSpace('ab c')); console.log(removeSpace('ab c')); console.log(removeSpace('ab c'));