Regex to replace multiple spaces with a single space

后端 未结 23 2322
北恋
北恋 2020-11-22 10:00

Given a string like:

\"The dog      has a long   tail, and it     is RED!\"

What kind of jQuery or JavaScript magic can be used to keep spaces to only o

23条回答
  •  北海茫月
    2020-11-22 10:21

    Here is an alternate solution if you do not want to use replace (replace spaces in a string without using replace javascript)

    var str="The dog      has a long   tail, and it     is RED!";
    var rule=/\s{1,}/g;
    
    str = str.split(rule).join(" "); 
    
    document.write(str);

提交回复
热议问题