Last updated: Mar 3, 2024
Reading timeยท3 min
Use the replace()
method to replace the first occurrence of a character in a
string.
The method takes a regular expression and a replacement string as parameters and returns a new string with one or more matches replaced.
const str = 'hello world'; const replaceFirst = str.replace(/l/, '_'); console.log(replaceFirst); // ๐๏ธ he_lo world const replaceAll = str.replace(/l/g, '_'); console.log(replaceAll); // ๐๏ธ he__o wor_d
We used the String.replace()
method to replace the first occurrence of the l
character in the string hello world
.
The String.replace() method returns a new string with one, some, or all matches of a regular expression replaced with the provided replacement.
The method takes the following parameters:
Name | Description |
---|---|
pattern | The pattern to look for in the string. Can be a string or a regular expression. |
replacement | A string used to replace the substring match by the supplied pattern. |
g
(global) flag after the regular expression.const str = 'hello world'; const replaceFirst = str.replace(/l/, '_'); console.log(replaceFirst); // ๐๏ธ he_lo world const replaceAll = str.replace(/l/g, '_'); console.log(replaceAll); // ๐๏ธ he__o wor_d
The forward slashes / /
mark the beginning and end of the regular expression.
The second parameter the replace
method takes is the replacement string.
In the example, we replaced the l
character with an underscore _
.
The String.replace()
method returns a new string with the matches of the
pattern replaced. The method doesn't change the original string.
Strings are immutable in JavaScript.
If you need to match the first occurrence of a character in a case-insensitive
manner, add the i
flag at the end of the regex.
const str = 'HELLO WORLD'; const replaceFirst = str.replace(/l/i, '_'); console.log(replaceFirst); // ๐๏ธ HE_LO WORLD const replaceAll = str.replace(/l/gi, '_'); console.log(replaceAll); // ๐๏ธ HE__O WOR_D
In this example, we used the i
flag to perform a case-insensitive search for
the l
character in the string and replaced it with an underscore.
replace()
methodYou can also use a string as the first argument of the replace()
method.
const str = 'hello world'; const replaceFirst = str.replace('l', '_'); console.log(replaceFirst); // ๐๏ธ he_lo world
The code sample replaces the first occurrence of l
in the string with an
underscore.
You can replace all occurrences of a character using a string argument by using
the replaceAll()
method.
const str = 'hello world'; const replaceAll = str.replaceAll('l', '_'); console.log(replaceAll); // ๐๏ธ he__o wor_d
The String.replaceAll() method returns a new string with all matches of a pattern replaced by the provided replacement.
The method takes the following parameters:
Name | Description |
---|---|
pattern | The pattern to look for in the string. Can be a string or a regular expression. |
replacement | A string used to replace the substring match by the supplied pattern. |
If you ever need help reading a regular expression, check out this regular expression cheat sheet by MDN.
It contains a table with the name and the meaning of each special character with examples.
You can learn more about the related topics by checking out the following tutorials: