%dw 2.0
output application/json
---
["123-456-7890" replace /.*-/ with(""), "abc123def" replace /[b13e]/ with("-")]
replace
replace(String, 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
Name | Description |
---|---|
|
A string to match. |
|
A Java regular expression for matching characters in the input |
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 (-
).
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.
replace(String, 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.