Replace the First Occurrence of Character in String in JS

avatar

Borislav Hadzhiev

3 min

banner

Photo from Unsplash

Replace the First Occurrence of a Character in a String in JS #

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.

index.js
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:

NameDescription
patternThe pattern to look for in the string. Can be a string or a regular expression.
replacementA string used to replace the substring match by the supplied pattern.
The difference between replacing the first match of a regex in a string and replacing all matches is specifying the g (global) flag after the regular expression.
index.js
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.

index.js
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.

You can also pass a string to the replace() method #

You can also use a string as the first argument of the replace() method.

index.js
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.

This is useful when you only want to replace the first occurrence of a character, but know the specific character ahead of time and don't have to match a pattern.

You can replace all occurrences of a character using a string argument by using the replaceAll() method.

index.js
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:

NameDescription
patternThe pattern to look for in the string. Can be a string or a regular expression.
replacementA 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 cheatsheet by MDN.

It contains a table with the name and the meaning of each special character with examples.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2023 Borislav Hadzhiev