The RegExpReplaceFirst function allows you to replace the first match for a regular expression in a string with a different string.
If a string matches a regular expression, you can change the first segment of the string that matched the regular expression into a different value. The RegExpReplaceFirst function always returns the target string, whether it was modified or not.
There are many web pages that help you test and debug your regular expressions, e.g. regex101.com.
In this example, the regular expression in A2 is matched against the string in A4. There is a match and the first segment that is matched is replaced with the new string in A6. The function returns the modified string.
This is what this RegExpReplaceFirst function call will look like:
=RegExpReplaceFirst(string, regex, replacement, flags)
The string is matched against the regular expression, controlled by the optional flags. The function and its results are modeled after the Substitute function in Excel:
If you need to know if a substitution was made, you must compare the original string with the result of the function, e.g.
=IF(A12=A4, "Unmodified", "Modified")
Specify the target string that you want to evaluate against the regular expression.
Specify the regular expression you wish to match. Do not use any special delimiter for the regex, e.g. forward slash.
A new string that replaces the first segment of the target string that matches the regular expression.
Specify the flags that control the evaluation of the regular expression (see below). The flags parameter is optional.
The way the function operates can be controlled by the flags below.
Global search, matches the pattern multiple times. Off by default.
Case insensitive search, both a and A match a. On by default.
Multi-line mode where ^ matches the start of the string and $ matches the end of the entire string. Without this, each line in the string is treated separately. On by default.
Short for single line; the . wildcard also matches newline characters. On by default.
Treats a pattern as a sequence of Unicode code points. On by default.
Perform a sticky search that matches starting at the current position in the target string. Off by default.