# Replace modifier

Learn how to replace substrings or patterns in a value.

This modifier replaces a target substring or pattern with a specified replacement.

Optionally, you can specify the occurrence of the pattern to replace and the offset to start the search for more control over the replacement. And for more advanced replacements, you can even use [regular expressions](/reference/cql/data-types/core/regex).

## Syntax

The basic syntax for this modifier is:

```cql
/*<value>*/ replacing /*<pattern>*/ with /*<replacement>*/
```

To replace a specific occurrence of the pattern, use the following syntax:

```cql
/*<value>*/ replacing /*<occurrence>*/ occurrence of /*<pattern>*/ with /*<replacement>*/
```

You can use any [ordinal](/reference/cql/expressions/literals/number#ordinals) to specify the occurrence, including the words *first*, *second*, *third*, and so on.

You can also prefix the ordinal with a definite article to make the expression more readable:

```cql
/*<value>*/ replacing the /*<occurrence>*/ occurrence of /*<pattern>*/ with /*<replacement>*/
```

To start the search at a specific offset, use this syntax:

```cql
/*<value>*/ replacing /*<pattern>*/ from /*<offset>*/ with /*<replacement>*/
```

## Parameters

These are the supported parameters:

- `value`: `string`

  The string in which to replace the pattern.

- `pattern`: `string | regex`

  A string or [regular expression](/reference/cql/expressions/literals/regex) to match.

- `replacement`: `string`

  A string to replace the matched pattern.

- `occurrence`: `ordinal` (optional) (default: all occurrences)

  An [ordinal literal](/reference/cql/expressions/literals/number#ordinals) indicating which occurrence of the matched pattern to replace.

- `offset`: `integer` (optional) (default: starts at the beginning of the string)

  A zero-based index indicating where to start the search for the pattern.

## Examples

Here is a basic example of how to replace a substring:

```cql
"Hello World" replacing "World" with "CQL" // Hello CQL
```

You can replace a specific occurrence of the pattern by specifying an ordinal:

```cql
"Hello World" replacing the first occurrence of "l" with "1" // He1lo World
```

To replace the last occurrence, use a negative ordinal:

```cql
"Hello World" replacing the last occurrence of "l" with "1" // Hello Wor1d
```

For more complex replacements, you can use [regular expressions](/reference/cql/expressions/literals/regex):

```cql
" Hello World  " replacing ~/^\s+|\s+$/ with "" // Hello World
```

The above example removes all leading and trailing whitespace from the string by replacing the pattern with an empty string.
