$ replace("string", 'pattern', "new", [n])

Replaces each substring of the specified string that matches the regular expression pattern with the specified new string.


"string" - the string to replace text in.

'pattern' - a regular expression pattern defining a substring that should be replaced.

"new" - the new text to replace each occurence of the pattern with. Within this string the literals $1, $2, etc. will be expanded to the pattern groupings captured by the pattern matcher. These are not Sleep variables, rather they are a special sequence interpreted by the regex engine.

n - if specified, only n occurences will be replaced. The default is to replace all matching substrings.


a scalar string


$string = replace("foo is the word, not bar!", '\Afoo|\Abar', "pHEAR"); println($string);

pHEAR is the word, not bar!

See Also