#!/usr/bin/ruby # ## https://rosettacode.org/wiki/Regular_expressions # var str = "I am a string"; Â # Substitute something mached by a regex str.sub!(/ a /, ' another '); # "I am a string" => "I am another string" Â # Remove something matched by a regex str -= / \Kanother /i; # "I am another string" => "I am string" Â # Global subtitution with a block str = str.gsub(/(\w+)/, {|s1| 'x' * s1.len}); # globaly replace any word with 'xxx' Â say str; # prints: 'x xx xxxxxx'