Reputation: 8511
How do I replace all non-word chars (\W) that are also not space characters (\s)?
This is the desired functionality:
"the (quick)! brown \n fox".gsub(regex, "#")
=>
"the #quick## brown \n fox"
Upvotes: 21
Views: 15869
Reputation: 19500
I think you need a regex like this one:
/[^\w\s]/
When you add a circumflex ^
to the start of a character set, it negates the expression so that anything except characters in the set are matched.
Upvotes: 11
Reputation: 2178
"the (quick)! brown \n fox".gsub(/[^\w\s]/, "#")
By making the regex replace anything that is NOT a word character OR a space character.
Upvotes: 31