Yes, Sorry I knew that, but got busy today, and could not catch up.
It is a AND scenario, but that expression uses OR.
This regex works better to find (ie “match”) the positions of non-word, and non-numeric and non-whitespace characters.
regex = /[^\w\d\s]/i
In Ruby regular expressions are filter patterns that some string methods use to return the positions of the matches within the string.
We need to use some other function/method or loop to go thru these matches, and process them (in your case replacing the matched characters.)
In Ruby, they have some nifty iterator methods that do this using the regex.
String#scan, String#gsub, String#tr, etc.
So the ticket is to find one that works in Javascript, which replace()
(with global search g
and i
ignorecase flags) looks to be the one. (It is equivalent to Ruby’s gsub() method.):
regex = /[^\w\d\s]/i
new_str = str.replace(regex, '_')
Now lets look back at what TIG wrote:
Which is a stripper RegExp and replaces special characters with nothing (just strips them out.)
I tested the following RegExp as a stripper in Ruby using gsub() (which doesn’t need a g
flag because it is the global substitution method.) Javascript has no exact equivalent and the replace method must be used with a RexExp that has a global replace g
flag.
rex = /[<>:"\/\\|?*]+/
(The slash and the backslash need escaping in Ruby, but the other characters like the pipe do not. But it doesn’t hurt to escape characters that don’t really need escaping.)