Regex to replace multiple spaces with a single space

后端 未结 23 2333
北恋
北恋 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

    We can use the following regex explained with the help of sed system command. The similar regex can be used in other languages and platforms.

    Add the text into some file say test

    manjeet-laptop:Desktop manjeet$ cat test
    "The dog      has a long   tail, and it     is RED!"
    

    We can use the following regex to replace all white spaces with single space

    manjeet-laptop:Desktop manjeet$ sed 's/ \{1,\}/ /g' test
    "The dog has a long tail, and it is RED!"
    

    Hope this serves the purpose

提交回复
热议问题