JavaScript: String replace() Method

The replace() method returns a new string with some or all matches of a pattern replaced by a replacement.

The first argument to the replace method is what you are searching for, expressed either as a string or a regular expression. The second argument is what you will replace it with. The second argument can be a string, or a function that returns the replacement string.

Syntax

Parameters
regexp (pattern) A RegExp object or literal. The match or matches are replaced with newSubStr or the value returned by the specified function.
substr (pattern) A String that is to be replaced by newSubStr. It is treated as a verbatim string and is not interpreted as a regular expression. Only the first occurrence will be replaced.
newSubStr (replacement) The String that replaces the substring specified by the specified regexp or substr parameter. A number of special replacement patterns are supported; see the “Specifying a string as a parameter” section below.
function (replacement) A function to be invoked to create the new substring to be used to replace the matches to the given regexp or substr. The arguments supplied to this function are described in the “Specifying a function as a parameter” section below.
Return value

A new string with some or all matches of a pattern replaced by a replacement.

Description

This method does not change the String object it is called on. It simply returns a new string.

1. String as replacement argument

The replacement string can include the following special replacement patterns:

Pattern Inserts
$$ Inserts a “$”.
$& Inserts the matched substring.
$` Inserts the portion of the string that precedes the matched substring.
$' Inserts the portion of the string that follows the matched substring.
$n Where n is a positive integer less than 100, inserts the nth parenthesized submatch string, provided the first argument was a RegExp object.

The following example insert 3 submatch strings "$1", "$2", "$3"

2. Function as replacement argument

In this case, the function will be invoked after the match has been performed. The function’s result (return value) will be used as the replacement string. Note that the function will be invoked multiple times for each full match to be replaced if the regular expression in the first parameter is global.

The arguments to the function are as follows:

Possible name Supplied value
match The matched substring. (Corresponds to $& above.)
p1, p2, ... The nth parenthesized submatch string, provided the first argument to replace() was a RegExp object. (Corresponds to $1, $2, etc. above.) For example, if /(\a+)(\b+)/, was given, p1 is the match for \a+, and p2 for \b+.
offset The offset of the matched substring within the whole string being examined. (For example, if the whole string was 'abcd', and the matched substring was 'bc', then this argument will be 1.)
string The whole string being examined.

The following example will set newString to ‘www-codingrepo-com’:

Leave a Reply

Your email address will not be published. Required fields are marked *

CAPTCHA