regex.replace_all()

regex.replace_all(haystack, pattern, replacement) replaces all occurrences of a regex pattern in a string with a replacement string.

Returns

  • if haystack and replacement are strings and pattern matches, returns a new string with all matches replaced by replacement.

  • otherwise, returns null.

Examples

// normalize multiple spaces
regex.replace_all(text, /\s+/, " ")
// redact sensitive data
regex.replace_all(log, /\d{3}-\d{2}-\d{4}/, "XXX-XX-XXXX")
// rewrite dates from MM/DD/YYYY to YYYY-MM-DD
regex.replace_all(dates, /(\d{2})\/(\d{2})\/(\d{4})/, "$3-$1-$2")

Last updated

Was this helpful?