---
schema_version: "1"
id: 9b5231d047ca2d597a1cedd484aea99d2821b90580d150773aa9df7199f1a8cc
title: replace
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/2.4/dw-core-functions-replace.md
canonical_url: https://docs.mulesoft.com/dataweave/2.4/dw-core-functions-replace
source_site: docs.mulesoft.com
content_type: documentation
content_hash: ea9d78f31c4bed5a7454e497fadb8304845e6ee39618280e1b561849ff94cbd6
description: MuleSoft Documentation Site
release: "2.4"
latest: false
---
---
product: DataWeave
version: 2.4
---

# replace

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

## replace(text: String, matcher: Regex): ((Array<String>, Number) -> String) -> String

Performs string replacement.

This version of `replace` accepts a Java regular expression for matching part of a string. It
requires the use of the `with` helper function to specify a replacement string for the matching part
of the input string.

### Parameters

<table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>text</code></p></td><td><p>A string to match.</p></td></tr><tr><td><p><code>matcher</code></p></td><td><p>A Java regular expression for matching characters in the input <code>text</code> string.</p></td></tr></tbody></table>

### Example

The first example in the source replaces all characters up to and including the second hyphen
(`123-456-`) with an empty value, so it returns the last four digits. The second replaces the
characters `b13e` in the input string with a hyphen (`-`).

#### Source

```DataWeave
%dw 2.0
output application/json
---
["123-456-7890" replace /.*-/ with(""), "abc123def" replace /[b13e]/ with("-")]
```

#### Output

```JSON
[ 7890, "a-c-2-d-f" ]
```

### Example

This example replaces the numbers `123` in the input strings with `ID`. It uses the regular
expression `(\d+)`, where the `\d` metacharacter means any digit from 0-9, and
`` ` means that the digit can occur one or more times. Without the ` ``, the output would contain
one `ID` per digit. The example also shows how to write the expression using infix notation, then
using prefix notation.

#### Source

```DataWeave
%dw 2.0
output application/json
---
[ "my123" replace /(\d+)/ with("ID"), replace("myOther123", /(\d+)/) with("ID") ]
```

#### Output

```JSON
[ "myID", "myOtherID" ]
```

## replace(text: String, matcher: String): ((Array<String>, Number) -> String) -> String

Performs string replacement.

This version of `replace` accepts a string that matches part of a specified string. It requires the
use of the `with` helper function to pass in a replacement string for the matching part of the input
string.

### Parameters

<table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>text</code></p></td><td><p>The string to match.</p></td></tr><tr><td><p><code>matcher</code></p></td><td><p>The string for matching characters in the input <code>text</code> string.</p></td></tr></tbody></table>

### Example

This example replaces the numbers `123` from the input string with the characters `ID`, which are
passed through the `with` function.

#### Source

```DataWeave
%dw 2.0
output application/json
---
{ "replace": "admin123" replace "123" with("ID") }
```

#### Output

```JSON
{ "replace": "adminID" }
```

## replace(text: Null, matcher: Any): ((Nothing, Nothing) -> Any) -> Null

Helper function that enables `replace` to work with a `null` value.

_Introduced in DataWeave version 2.4.0._
