Backslash Sequences in the Substitution : Regexps Group « Development « Ruby






Backslash Sequences in the Substitution



"fred:smith".sub(/(\w+):(\w+)/, '\2, \1') # "smith, fred" 
"nercpyitno".gsub(/(.)(.)/, '\2\1') # "encryption" 

 








Related examples in the same category

1.An unescaped vertical bar(|) matches either the regular expression that precedes it or the regular expression that follows it.
2.use parentheses to group terms within a regular expression
3.\1 refers to the match of the first group
4.unescape HTML