I'm trying to replace occurrences of special characters like /, :, etc in a string with */*, *:* respectively.
str.gsub!(/([;.\\/?:@&=+$,{}|^\[\]`<>#%"'])/, '*\1*')
However, I get a SyntaxError:
`@&' is not allowed as an instance variable name
syntax error, unexpected end-of-input, expecting tSTRING_CONTENT or tSTRING_DBEG or tSTRING_DVAR or tSTRING_END
str.gsub!(/([;.\\/?:@&=+$,{}|^\[\]`<>#%"'])/, '*...
^ (SyntaxError)
I tried to define that regexp as a string and use RegExp.quote(...) to convert it, but to no avail. Any suggestions would be highly appreciated!
/symbol in the regex:str.gsub!(/([;.\\\/?:@&=+$,{}|^\[\]`<>#%"'])/, '*\1*'). RoR expects a valid variable name after@in your case.