string.replace()

Replace a specified number of occurrences of a substring in a String.

Signature

replace(pattern: String, replacement: String) => String

replace(pattern: String, replacement: String, amount: Number) => String

Description

Replaces the occurrences of a provided substring in the calling String with a provided replacement for a specified number of times.

Returns a new String. The calling String isn’t changed.

Parameters

Parameter Type Required Description

pattern

String

true

Substring to match in the calling String.

replacement

String

true

Replacement for matching substrings in the calling String.

amount

Number

Number of replacements to make in the calling String. Defaults to 1.

Return value

Type Description

String

Resulting String with replacements.

Examples

'foobar'.replace('foo', 'bar')
"barbar"
\