• Text before first match is kept.
  • Text after last match is kept.
  • Regular expression must also "match" all characters between first and last match.


  • 3 matches "per loop", so replace() works as wanted, but match() "looks bad".
  • It's impossible to keep the 2 "Hello" between first and last match.


  • Only one match "per loop", so match() looks "cool", but replace() finds only 1 parameter instead of 3.


  • 2 matches per loop, so "Hello" and "World" can be swapped.
  • "Clearer" example to understand how works $1 .. $9, to be used in the second replace(...,...) parameter.