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

> ## Agent Instructions
> FalkorDB is a graph database that speaks the Redis protocol. Queries are issued as OpenCypher through the GRAPH.QUERY and GRAPH.RO_QUERY commands, not over Bolt or a SQL connection.
> FalkorDB implements a subset of OpenCypher with proprietary extensions. Do not assume Neo4j-only syntax or procedures are available — check /cypher/cypher-support and /cypher/known-limitations before using a clause.
> FalkorDB is the successor to RedisGraph, but they are separate products. Do not present RedisGraph commands, versions, or limitations as current FalkorDB behavior.
> Use the official clients listed in /getting-started/clients rather than generic Redis or Neo4j drivers, and prefer the language the user is already working in.
> Configuration parameters are set with GRAPH.CONFIG SET or at startup; cite the exact parameter name from /getting-started/configuration rather than inventing one.
> This site covers four products: FalkorDB (core), FalkorDB Cloud, FalkorDB Enterprise, and the GraphRAG SDK. Name which one an answer applies to, since setup and operations differ.

# 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>
