Replace all non Alpha Numeric characters, New Lines, and multiple White Space with one Space

后端 未结 8 1634
终归单人心
终归单人心 2021-01-29 20:24

I\'m looking for a neat RegEx solution to replace

  • All non Alpha-Numeric Characters
  • All NewLines
  • All multiple instances of white space
8条回答
  •  灰色年华
    2021-01-29 20:38

    Be aware, that \W leaves the underscore. A short equivalent for [^a-zA-Z0-9] would be [\W_]

    text.replace(/[\W_]+/g," ");
    

    \W is the negation of shorthand \w for [A-Za-z0-9_] word characters (including the underscore)

    Example at regex101.com

提交回复
热议问题