Skip to main content

REGEX_REPLACE

Arnaud Lachaume avatar
Written by Arnaud Lachaume
Updated yesterday

Type: Function

Function: REGEX_REPLACE

Syntax: REGEX_REPLACE(string, regex, replacement)

Description: Replaces part of a text string with a different text string using regular expressions. The regex must be a POSIX regular expression. You can use this tool to test your regular expressions

Numbered regex captures (\1, \2) can be used to insert captured text into the replacement string (see example below). And since backslashes need to be escaped, you will have to use \\1 and \\2 in strings, such as REGEX_REPLACE("foo123", "(\\d+)", "_\\1")

Similarly, typical regex patterns such as \d will have to be written \\d in regex strings. See the examples below.

Sample usage:

  • REGEX_REPLACE("foo123", "\\d+", "_some_numbers") returns "foo_some_numbers".

  • REGEX_REPLACE("foo123", "(\\d+)", "_\1") returns "foo_123".

  • REGEX_REPLACE(author_username, "bot$", "robot") will replace the ending bot by robot for any author ending with bot. For instance, "dependabot" will become "dependarobot" but botify will remain intact since it does not end with bot.


​Regular Expression cheat sheet.:

  • "^fix" string starts with "fix"

  • "fix$" string ends with "fix"

  • "fix|bug" string contains "fix" or "bug"

  • Test more regular expressions using this tool

Did this answer your question?