Search code examples
javascriptregexreplacealphanumeric

Replace all non alphanumeric characters, new lines, and multiple white space with one space


I'm looking for a neat regex solution to replace

  • All non alphanumeric 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 I'd need are

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

However, I can't 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

Solution

  • 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