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

# RegExp

> Rewrite a value with a regular expression.

The **RegExp** formatter rewrites a value using a regular expression: it finds what the pattern matches and replaces it with your replacement. Use it to strip prefixes, remove separators, or pull one part out of a longer value.

```
INV-1023         ->   1023            (match "INV-", no replacement)
NO 123 456 789   ->   NO123456789     (match "\s", no replacement)
```

## Options

| Option | What it does |
| - | - |
| **Match pattern** | Required. A JavaScript regular expression. Everything it matches is replaced. |
| **Replace pattern** | What to put in place of each match. Leave it empty to delete the match. Capture groups are available as `$1`, `$2`, and so on. |
| **Match flags** | JavaScript regular expression flags. Defaults to `gs` — `g` replaces every match rather than just the first, `s` lets `.` match newlines. |

## Examples

| Goal | Match pattern | Replace pattern | `Total: 1234 NOK` becomes |
| - | - | - | - |
| Strip a prefix | `INV-` | *(empty)* | — |
| Remove all spaces | `\s` | *(empty)* | `Total:1234NOK` |
| Keep only the number | `^\D*(\d+).*$` | `$1` | `1234` |
| Swap separators | `-` | `/` | — |

<Tip>
  This formatter **replaces** rather than extracts. To keep only part of a value, write a pattern that matches the whole value, put the part you want in a capture group, and use `$1` as the replacement — as in the third row above.
</Tip>

## When it fails

If the pattern or the flags are not valid, the field is cleared and the formatter reports `RegEx Formatter: Invalid pattern or flags specified`, which sends the document to human review. A pattern that simply does not match anything is not an error — the value is passed through unchanged. An empty value is left empty.

<Note>
  This is the formatter, which rewrites values. To check a value against a pattern without changing it, use the [RegExp validator](/core-concepts/ai-model/regex-validator) instead.
</Note>
