I'm looking for a neat RegEx solution to replace
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 insensitiveHowever, 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
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)