For example:
str1 = "pppp(m)pppp"
str2 = "(m)"
str1 = str1.sub(/#{str2}/, "<>#{str2}<>")
I will got this:
"pppp(<>(m)<>)pppp"
I expected to get this:
"pppp<>(m)<>pppp"
Why it’s happening and how to avoid this?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In
(and)have a special meaning in regexen and do not actually match the characters(and). The regex/(m)/will match anymwhether or not it is enclosed in parentheses (and if it is, it won’t match the parentheses).To match literal parentheses use
\(and\)– or in a case like this where you’re interpolating a string, you can just useRegexp.escapeon the string, i.e./#{ Regexp.escape(str2) }/.