Reputation: 46379
I have an expression like this:
s.gsub! /[\?\/\\]/, ''
In fact, the list of forbidden characters is longer than that. Some require escaping, some don't. Is there a way I could just put the characters in some literal structure (?/\
) and say "remove all these from the string". I'm aware of Regexp.quote but not sure how to use it in this context.
Upvotes: 2
Views: 1039
Reputation: 176352
You can interpolate a statement inside a Ruby regular expression, exactly like you do for strings.
/#{...}/
In your case
s.gsub! /[#{Regexp.escape('?/\')}]/, ''
Upvotes: 3