Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
427 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 Answer

0 votes
by (71.8m points)

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...