For example, with input string 'Hello World' this replacement:
mystring.sub(/(Wo)rld/, '\1bble') -> Hello WobbleNote the single quotes - if you use double quotes this happens:
mystring.sub(/(Wo)rld/, "\1bble") -> Hello \u0001bbleYou can use double quotes if you double escape the 'sequence' \1
mystring.sub(/(Wo)rld/, "\\1bble") -> Hello WobbleThis is very important if you want to include a ruby variable in the replacement string
s = "Mr." mystring.sub(/(Wo)rld/, "#{s} \\1bble") -> Hello Mr. WobbleJust remember 'double quotes' means 'double escapes'....
No comments:
Post a Comment