In answering this PHP question: regex – preg_replace string, I came across something in Javascript I didn’t understand.
Given the following:
var s = "abc1!?d$";
alert(s.replace(/\W+/, " "));
I am alerted:
abc d$
Why is it not stripping out the last dollar?
Because there’s an intervening word character. Try this:
Without the “g” suffix on the regex, it only makes one substitution. That handles the “!?” in the middle, but that “d” ends the sequence.