# REGEXREPLACE

## Content

## Summary

The `REGEXREPLACE` function replaces text in a string based on a specified regular expression pattern.

## Syntax

```javascript
=REGEXREPLACE(text, pattern, replacement, [occurrence], [case_sensitivity])
```

## Arguments

| Argument | Description |
| -------- | ----------- |
| **text**<br>(required) | The input text or cell reference containing text to replace. |
| **pattern**<br>(required) | Regular expression pattern to match. |
| **replacement**<br>(required) | Text to substitute for matched patterns |
| **occurrence** | Specifies which match to replace:<br>`0`: Replace all (default)<br>Positive N: Replace Nth occurrence<br>Negative N: Replace Nth occurrence from end |
| **case\_sensitivity** | Case handling:<br>`0`: Case-sensitive (default)<br>`1`: Case-insensitive |

## Remarks

* **Returns:** The modified string after replacements

## Examples
```auto
=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:<br>`=REGEXREPLACE("aaaAbcd","a","0", ,1)` | Uses string modifiers (e.g., "ig"):<br>`=SJS.REGEXREPLACE("aaaAbcd","a","0","ig")` |
| **Instance Replacement** | Flexible occurrence control:<ul><li>`0`: Replace all (default)</li><li>Positive N: Replace Nth occurrence</li><li>Negative N: Replace Nth occurrence from end</li></ul>`=REGEXREPLACE("aaaAbcd","a","0", 2,1)`<br>`=REGEXREPLACE("aaaAbcd","a","0", -3,1)` | Limited to:<ul><li>First instance (default)</li><li>All instances (with "g" modifier)</li></ul> |
| **Multiline Handling** | Requires standard regex syntax:<br>`=REGEXREPLACE("abc`<br>`ABC","\n[A-Z]+","*")` | Supports "m" modifier for multiline:<br>`=SJS.REGEXREPLACE("abc`<br>`ABC","^[A-Z]+","*","m")` |
| **Non-String Inputs** | Accepts all data types:<br>`=REGEXREPLACE(2024,24,25)` | Returns `#VALUE!` for non-string inputs |
