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

The General picture The General · Jan 1, 2014 · Viewed 152.2k times · Source

I'm looking for a neat RegEx solution to replace

  • All non Alpha-Numeric Characters
  • All NewLines
  • All multiple instances of white space

With a single space


For those playing at home (the following does work)

text.replace(/[^a-z0-9]/gmi, " ").replace(/\s+/g, " ");

My thinking is RegEx is probably powerful enough to achieve this in one statement. The components i think id need are

  • [^a-z0-9] - to Remove non Alpha-Numeric characters
  • \s+ - match any collections of spaces
  • \r?\n|\r - match all new line
  • /gmi - global, multi-line, case insensitive

However, i cant seem to style the regex in the right way (the following doesn't work)

text.replace(/[^a-z0-9]|\s+|\r?\n|\r/gmi, " ");


Input

234&^%,Me,2 2013 1080p x264 5 1 BluRay
S01(*&asd 05
S1E5
1x05
1x5


Desired Output

234 Me 2 2013 1080p x264 5 1 BluRay S01 asd 05 S1E5 1x05 1x5

Answer

Jonny 5 picture Jonny 5 · Jan 1, 2014

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