Ruby - Escape Parenthesis

Posted by Todd Horrtyz on Stack Overflow See other posts from Stack Overflow or by Todd Horrtyz
Published on 2010-05-06T18:14:42Z Indexed on 2010/05/06 18:18 UTC
Read the original article Hit count: 312

Filed under:
|

I can't for the life of me figure this out, even though it should be very simple.

How can I replace all occurrences of "(" and ")" on a string with "\(" and "\)"?

Nothing seems to work:

"foo ( bar ) foo".gsub("(", "\(") # => "foo ( bar ) foo"

"foo ( bar ) foo".gsub("(", "\\(") # => "foo \\( bar ) foo"

Any idea?

© Stack Overflow or respective owner

Related posts about ruby

Related posts about ruby-on-rails