I’m trying to learn RegEx in Ruby, based on what I’m reading in ‘The Rails Way‘. But, even this simple example has me stumped. I can’t tell if it is a typo or not:
text.gsub(/\s/, '-').gsub([^\W-], '').downcase
It seems to me that this would replace all spaces with -, then anywhere a string starts with a non letter or number followed by a dash, replace that with ”. But, using irb, it fails first on ^:
syntax error, unexpected ‘^’, expecting ‘]’
If I take out the ^, it fails again on the W.
Missing //
Although this makes a little more sense 🙂
And this is probably what is meant
\W means ‘not a word’ \w means ‘a word’
The // generate a regexp object