> ## Documentation Index
> Fetch the complete documentation index at: https://new.docs.falkordb.com/llms.txt
> Use this file to discover all available pages before exploring further.

# text.replace

> Replaces all occurrences of a regex pattern in a string with a replacement string.

## Description

Replaces all occurrences of a substring matching a regular expression pattern with a replacement string.

## Syntax

```cypher theme={null}
flex.text.replace(string, regex, replacement)
```

## Parameters

| Parameter     | Type   | Required | Description                                                |
| ------------- | ------ | -------- | ---------------------------------------------------------- |
| `string`      | string | Yes      | The string to perform replacements on                      |
| `regex`       | string | Yes      | The regular expression pattern to match (applied globally) |
| `replacement` | string | Yes      | The string to replace matches with                         |

## Returns

**Type:** string

A new string with all pattern matches replaced by the replacement string. Returns `null` if input string is `null`.

## Examples

### Example 1: Basic Text Replacement

```cypher theme={null}
RETURN flex.text.replace('hello world', 'world', 'universe') AS result
```

**Output:**

```text theme={null}
result
--------------
hello universe
```

### Example 2: Remove Non-Numeric Characters

```cypher theme={null}
WITH 'Phone: (555) 123-4567' AS phone
RETURN flex.text.replace(phone, '[^0-9]', '') AS cleaned
```

**Output:**

```text theme={null}
cleaned
-----------
5551234567
```

### Example 3: Sanitize User Input

```cypher theme={null}
MATCH (c:Comment)
WITH c, flex.text.replace(c.text, '<[^>]+>', '') AS sanitized
RETURN sanitized AS cleanComment
```

### Example 4: Normalize Whitespace

```cypher theme={null}
WITH '  Multiple   spaces   here  ' AS text
RETURN flex.text.replace(text, '\\s+', ' ') AS normalized
```

**Output:**

```text theme={null}
normalized
-------------------------
 Multiple spaces here
```

## Notes

* Returns `null` if input string is `null`
* Uses global replacement (replaces all occurrences)
* Pattern is treated as a regular expression
* Useful for data cleaning, sanitization, and text transformation
* Can use regex patterns for complex replacements

## See Also

* [text.regexGroups](/udfs/flex/text/regexGroups) - Extract matches with capture groups
* [text.indexOf](/udfs/flex/text/indexOf) - Find substring position
* [text.format](/udfs/flex/text/format) - Format strings with placeholders

## Frequently Asked Questions

<AccordionGroup>
  <Accordion title="Does flex.text.replace use regex or literal matching?">
    It uses **regex** pattern matching. If you want a literal match, escape any special regex characters in your pattern.
  </Accordion>

  <Accordion title="Does it replace all occurrences or just the first?">
    It replaces **all** occurrences (global replacement).
  </Accordion>
</AccordionGroup>
