d-_*_*_-b 33 javascript regex trim
什么功能会转
this contains spaces成
this contains spaces用javascript?
我尝试了以下,使用类似的SO问题,但无法使其工作.
var string = " this contains spaces ";
newString = string.replace(/\s+/g,''); // "thiscontainsspaces"
newString = string.replace(/ +/g,''); //"thiscontainsspaces"
Run Code Online (Sandbox Code Playgroud)
Ham*_*ish 84
你很亲密
请记住,replace
用第二个参数替换找到的文本.所以:
newString = string.replace(/\s+/g,''); // "thiscontainsspaces"
Run Code Online (Sandbox Code Playgroud)
查找任意数量的连续空格并将其删除.尝试用一个空格替换它们!
newString = string.replace(/\s+/g,' ').trim();
Run Code Online (Sandbox Code Playgroud)
试试这个,这将替换字符串中的 2 或 2+ 个空格。
const string = " this contains spaces ";
string.replace(/\s{2,}/g, '').trim()
Run Code Online (Sandbox Code Playgroud)
我想出了一种方法,但很好奇是否有更好的方法......
string.replace(/\s+/g,' ').trim()
Run Code Online (Sandbox Code Playgroud)
小智 6
我遇到了同样的问题,我是这样解决的
Text = Text.replace(/ {1,}/g," ");
Text = Text.trim();
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
37933 次 |
最近记录: |