How To Replace All Occurrences Of A String In JavaScript – Definitive Guide

In JavaScript, String is an Object used to store, represent and manipulate the sequence of characters in a textual form.

JavaScript provides multiple options to replace all occurrences of a String and str.replace(new RegExp('searchstr', 'g'), 'replacestr'); is one among them and can be used for performance critical use cases.

If you are in a hurry
You can use the below code of str.replace() option and regular expression to replace all occurrences of a string in JavaScript.
Code

    const str = "fire in the mountain, well well well";

    const newStr = str.replace(/well/g, 'run');
    console.log(newStr);

Output

    fire in the mountain, run run run

There are a couple of other ways available to replace all occurrences of a string in JavaScript, and let us see those in detail.

Using replace() with Regular expression

String replace() method combined with Regular expression is considered the fastest option to replace all occurrences of a String.

Using replace() is also recommended in performance-critical use cases.

  • Short String use cases, you can use str.replace(/pattern/g, 'replace string');
  • Long String use cases, you can use str.replace(new RegExp('pattern', 'g'), 'run');

Case-sensitive string replace
Code

    const str = "fire in the mountain, well WELL well";

    const newStr = str.replace(new RegExp('well', 'g'), 'run');
    console.log(newStr);

Output

    fire in the mountain, run WELL run

The regular expression object(RegExp) with global search('g') enabled is used to find all the occurrences of string(well) and replace them with the string run using the string replace method.

You can see the above is case sensitive approach, so the WELL text in the string is not replaced.

Case insensitive string replace
You have to add the i flag to the RegEx search to replace all occurrences of a string in a case insensitive way,
Code

    const str = "fire in the mountain, well WELL well";

    const newStr = str.replace(new RegExp('well', 'gi'), 'run');
    console.log(newStr);

Output

    fire in the mountain, run run run

All the occurrences of string(well) are replaced, ignoring the case.

Using replace()

String replace method returns a new string replacing all the matches of a pattern.

Note:

  • This will only replace the first occurrence if the pattern used to replace is a string.
  • If you want to replace all occurrences of a string, you must follow Using replace() with Regular expression.

Code

    const str = "fire in the mountain, well WELL well";

    const newStr = str.replace("well", "run");
    console.log(newStr);

Output

    fire in the mountain, run WELL well

You can see the search pattern is a string("well"), and as a result, only the first occurrence of the search string(well) got replaced with run.

Using replaceAll()

StringreplaceAll method will replace all occurrences of a string with the replace string.
Code

    const str = "fire in the mountain, well WELL well";

    const newStr = str.replaceAll(new RegExp('well','g'), "run");
    console.log(newStr);

Output

    fire in the mountain, run WELL run

Using replaceAll() works in a case-sensitive approach, so exact match strings alone will be replaced.

Note:
While using RegEx, it is required to set a global(‘g’) flag; else, it will throw TypeError: "replaceAll must be called with a global RegExp".

Using split() and join()

The string split and join approach to replace all occurrences of a string is an alternate approach but slower than RegEx.

Code

    const str = "fire in the mountain, well WELL well";

    const newStr = str.split("well").join("run");
    console.log(newStr);

Output

    fire in the mountain, run WELL run

First, you need to split() the string based on the searchString(well) and create an array of strings.
["fire in the mountain, ", " WELL ", ""]
Now use join() to join the array combining the replaceString(run).
"fire in the mountain, run WELL run"

Note: This is case sensitive approach.

Live Demo

Summary

You have learned how to replace all occurrences of a string in JavaScript.
You have also learnt about different ways to replace all occurrences of a string using replace(), replaceAll() and split().join() and its use.

You May Also Like

Leave a Comment