Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby regex: replace non-word chars that are not space chars

Tags:

regex

ruby

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"

like image 779
bevanb Avatar asked Apr 20 '12 21:04

bevanb


1 Answers

"the (quick)! brown \n fox".gsub(/[^\w\s]/, "#")

By making the regex replace anything that is NOT a word character OR a space character.

like image 114
Chris Zelenak Avatar answered Sep 23 '22 13:09

Chris Zelenak