chickadee » irregex » irregex-replace

(irregex-replace <irx> <str> [<replacements> ...])procedure
(irregex-replace/all <irx> <str> [<replacements> ...])procedure

Matches a pattern in a string, and replaces it with a (possibly empty) list of substitutions. Each <replacement> can be either a string literal, a numeric index, a symbol (as a named submatch), or a procedure which takes one argument (the match object) and returns a string.

Examples:

(irregex-replace "[aeiou]" "hello world" "*") => "h*llo world"

(irregex-replace/all "[aeiou]" "hello world" "*") => "h*ll* w*rld"

(irregex-replace "(.)(.)" "ab" 2 1 "*")  => "ba*"

(irregex-replace "...bar" "xxfoobar" (lambda (m) 
              (string-reverse (irregex-match-substring m)))) => "xxraboof"

(irregex-replace "(...)(bar)" "xxfoobar"  2 (lambda (m) 
              (string-reverse (irregex-match-substring m 1)))) => "xxbaroof"