JavaScript replace method

The replace method of JavaScript is used to match and replace a value (regular expression or string) in a given string.

The value can be a simple string or regex object (regular expression). The replace method returns a string which is the replaced string if a match is found.

See a replace method example

In this JS replace tutorial we will show you examples of both i.e. string value and regex.

Syntax of replace string method

Following is the general syntax of JavaScript replace string method:

Str.replace(“existing_substring” , string/regex);

Where

  • Str is a string variable.
  • existing_substring is a string in the Str to be replaced.
  • String/regex can be a new string or regular expression that will replace the existing substring.

JavaScript string replace example

Following is a replace method example with a string value. In this example, we simply created a string variable with a sentence “This is javascript string replace tutorial!”.

After that, we used the replace method and replaced substring “javascript” to “JS”, that will be shown in an alert as you click the button.

Experience this example online

As you can see the alert showing “This is JS string replace tutorial!” after using the replace method.

A string replace example with regex

Following is string replace example with a regular expression. In this example, we will use a simple regular expression without any modifier. Same string as in the above example is used.

Experience this example online

After clicking the button the alert shows replaced string.

JavaScript replace all with global regex modifier

In above examples, with string or regex without a modifier, the replace method only changed first matched string.

Suppose you have multiple occurrences of the string and you want to replace all with a given string. You can use the global modifier in the replace method to replace all instances of the matched string as follows:

Experience this example online

You can see, the “javascript” is used twice in the given string (str). We used a regular expression with global modifier /javascript/g.

This changed the original string (This is javascript string replace tutorial! javascript” to (This is JS string replace tutorial! JS).

If we did not use the /g, only the first occurrence of JavaScript would have replaced. So you can use a global modifier in the regex to replace all instances.

JavaScript regex replace with case-insensitive modifier example

Following is a JavaScript regex replace example with the case-insensitive modifier. If you use regex in the replace method, it is case-sensitive.

In order to use replace with case-insensitive, you can use the regex modifier /i. See the following example:

Experience this example online

The given string is using Javascript (capital J). While in the replace method we provided “javascript” with the small letter. It still replaced the string that we have given in regex, as we used /i modifier; that specifies to make it case insensitive.