[]
The REGEXREPLACE
function replaces text in a string based on a specified regular expression pattern.
=REGEXREPLACE(text, pattern, replacement, [occurrence], [case_sensitivity])
Argument | Description |
---|---|
text (required) | The input text or cell reference containing text to replace. |
pattern (required) | Regular expression pattern to match. |
replacement (required) | Text to substitute for matched patterns |
occurrence | Specifies which match to replace:
Positive N: Replace Nth occurrence Negative N: Replace Nth occurrence from end |
case_sensitivity | Case handling:
|
Returns: The modified string after replacements
=REGEXREPLACE("0000abcd","0","1", 2) // 0100abcd
=REGEXREPLACE("0000abcd","0","1", 0) // 1111abcd
=REGEXREPLACE("0000abcd","0","1", -1) // 0001abcd
=REGEXREPLACE("aaaAbcd","a","0", ,0) // 000Abcd
=REGEXREPLACE("aaaAbcd","a","0", ,1) // 0000bcd
=REGEXREPLACE("Sonia (378) 555-4195 Brown(878) 555-8622", "[0-9]+-", "***-") // Sonia (378) ***-4195 Brown(878) ***-8622
=REGEXREPLACE("JamesHenry","([A-Z][a-z]+)([A-Z][a-z]+)","$2, $1") // Henry, James
The REGEXREPLACE function maintains compatibility with Excel's implementation while replacing the legacy SJS.REGEXREPLACE function. Although deprecated, SJS.REGEXREPLACE remains backward compatible. Key differences between the two functions include:
Feature | REGEXREPLACE Function | SJS.REGEXREPLACE Function |
---|---|---|
Argument Types | Uses numeric values (0/1) for case sensitivity:
| Uses string modifiers (e.g., "ig"):
|
Instance Replacement | Flexible occurrence control:
| Limited to:
|
Multiline Handling | Requires standard regex syntax:
| Supports "m" modifier for multiline:
|
Non-String Inputs | Accepts all data types:
| Returns |