Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
432 views
in Technique[技术] by (71.8m points)

javascript - 用JavaScript字符串中的单个空格替换多个空格(Replace multiple whitespaces with single whitespace in JavaScript string)

I have strings with extra whitespaces, each time there's more than only one whitespace I'd like it be only one.(我有额外的空格字符串,每次只有一个空格我想它只有一个。)

Anyone?(任何人?)

I tried searching google, but nothing worked for me.(我试着搜索谷歌,但没有任何对我有用。)

Thanks(谢谢)

  ask by eve translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Something like this:(像这样的东西:)

 var s = " abc "; console.log( s.replace(/\s+/g, ' ') ) 


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...