replace() JavaScript String Method – How to Replace Text Strings
Whenever you use replace() on a string, the method does the following:
- It searches the string for a given pattern.
- It substitutes the pattern found with a new value—thereby creating a new string.
- It returns the new version of the calling string—without changing the original string.
Syntax of the replace()
Method
Here is replace()
’s syntax:
The snippet above shows that replace()
accepts two arguments: a searchPattern and a newValue.
Argument 1: searchPattern
A searchPattern
is the first argument accepted by the replace()
method. It is used to specify the pattern you want to replace in the callingString
.
Keep in mind that you can use a string or a regular expression as the searchPattern
.
Suppose the searchPattern
argument is a string. In such a case, the computer will replace the first instance only.
To replace multiple instances of a pattern, use a regular expression with a g
(global) flag as the searchPattern
.
Argument 2: newValue
A newValue
is the second argument accepted by the replace()
method. It specifies the value you want to use to replace the searchPattern
found in the callingString
.
Below are some examples:
Example 1: Replace Friday
with Sunday
The snippet above replaced only the first "Friday"
pattern of the calling string because we used a string as the searchPattern
. But you can also use a regular expression pattern for global replacement. Let’s see an example below.
Example 2: Global Replacement of Friday
with Sunday
To replace all occurrences of a "Friday"
pattern, use a regular expression with a g
(global) flag like so:
Let’s now see how to do a case-insensitive replacement.
Example 3: Global Case-Insensitive Replacement of walk
with Jogg
Whenever you need to do a case-insensitive replacement, add an i
(ignore case) flag to your regular expression like this:
Important Stuff to Know about the replace()
Method
Keep these two essential pieces of info in mind whenever you choose to use the replace()
method.
- If
replace()
’s second argument is a string, you can include some special replacement patterns. - You can use a function as
replace()
’snewValue
argument.
Let’s discuss the above points.
How to Use Special Replacement Patterns when replace()
’s newValue
Argument Is a String
JavaScript has some special patterns that you can use if replace()
’s second argument is a string. Let’s talk about some of the special patterns below.
$$
Use $$
to insert a dollar sign.
Here’s an example:
$&
Use $&
to reference the matched pattern.
Here’s an example:
$`
Use $`
to reference the portion of the calling string that precedes the matched string.
Here’s an example:
$'
Use $'
to reference the portion of the calling string that succeeds the matched string.
Here’s an example:
$n
Use $n
to recall a captured group, where n
is the number that the computer assigned to the capturing group.
Example 1: Insert space between capturing groups 1 and 2
Example 2: Swap patterns
Design in Figma, launch in Webflow
$<Name>
Use $<Name>
to recall a named captured group, where <Name>
is the name assigned to the capturing group.
Example 1: Insert space between the named capturing group 1 and 2
Example 2: Swap named patterns
Let’s now discuss using a function as replace()
’s newValue
argument.
How to Use a Function as replace()
’s newValue
Argument
JavaScript allows you to use a function as replace()
’s second argument.
If the newValue
argument is a function, the computer will do the following:
- It will invoke the function after each match.
- It will use the function’s returned value to replace the pattern found.
Let’s now discuss the syntax of the function argument.
Syntax of a replace()
method’s function argument
Here is the syntax of replace()
’s function argument:
As shown in the snippet above, replace()
’s function argument accepts one or more parameters.
currentMatch
The currentMatch
parameter represents the matched pattern the computer is currently processing.
Example 1: Change code
from lowercase to uppercase
Example 2: Prepend walk
with Jogg-
Let’s now discuss the p1, ..., pX
parameters.
Design in Figma, launch in Webflow
p1, ..., pX
The p1, ..., pX
parameters are optional. They represent the pattern found by the nth capturing group of the searchPattern
argument.
Example 1: Replace Walk789
Example 2: Replace $1m shops
and Walk789
Let’s now discuss the offset
parameter.
offset
The offset
parameter is optional. It represents the offset position of the matched pattern within the entire calling string.
For instance, suppose the calling string is "Gorgeous"
, and the matched pattern is "geo"
. In that case, 3
will be the offset
parameter’s value because "geo"
started from the 3rd index position.
Example 1: Replace daily
Example 2: Replace 900xxxx
Let’s now discuss the string
parameter.
string
The string
parameter is optional. It represents the entire calling string.
Example 1: Replace CodeSweetly
Learn Flexbox with Images
Example 2: Replace 7000 times
Let’s now discuss the groups
parameter.
groups
The groups
parameter is optional. It represents a collection of the named capturing groups you specified in the searchPattern
’s regular expression. Or undefined
if the regular expression does not contain any named capturing group.
Here’s an example: