JavaScript Tutorials

JavaScript String replace()
Syntax & Examples

String.replace() method

The replace() method of the String class in JavaScript is used to replace occurrences of a specified pattern with a replacement string or the result of a function.


Syntax of String.replace()

The syntax of String.replace() method is:

replace(pattern, replacement)

This replace() method of String used to replace occurrences of searchFor using replaceWith. searchFor may be a string or Regular Expression, and replaceWith may be a string or function.

Parameters

ParameterOptional/RequiredDescription
patternrequiredThe pattern to search for. It may be a string or a regular expression.
replacementrequiredThe replacement string or function to use when replacing occurrences of the pattern.

Return Type

String.replace() returns value of type String.



✐ Examples

1 Using replace() method to replace a substring

In JavaScript, the replace() method can be used to replace occurrences of a substring with another string.

For example,

  1. We define a string variable str with the value 'hello world'.
  2. We use the replace() method to replace occurrences of 'world' with 'universe'.
  3. The result is stored in the variable newStr.
  4. We log newStr to the console using the console.log() method.

JavaScript Program

const str = 'hello world';
const newStr = str.replace('world', 'universe');
console.log(newStr);

Output

hello universe

2 Using replace() method with a regular expression

In JavaScript, the replace() method can use a regular expression as the pattern to replace occurrences of a specified pattern with a replacement string.

For example,

  1. We define a string variable str with the value 'The quick brown fox jumps over the lazy dog'.
  2. We use the replace() method with a regular expression to replace all occurrences of vowels with '*'.
  3. The result is stored in the variable newStr.
  4. We log newStr to the console using the console.log() method.

JavaScript Program

const str = 'The quick brown fox jumps over the lazy dog';
const newStr = str.replace(/[aeiou]/ig, '*');
console.log(newStr);

Output

Th* q**ck br*wn f*x j*mps *v*r th* l*zy d*g

3 Using replace() method with a function

In JavaScript, the replace() method can use a function as the replacement. The function is invoked for each match found, and its return value is used as the replacement.

For example,

  1. We define a string variable str with the value 'apple, banana, cherry'.
  2. We use the replace() method with a function to replace each fruit with its uppercase version.
  3. The result is stored in the variable newStr.
  4. We log newStr to the console using the console.log() method.

JavaScript Program

const str = 'apple, banana, cherry';
const newStr = str.replace(/\b\w+\b/g, function(match) {
  return match.toUpperCase();
});
console.log(newStr);

Output

APPLE, BANANA, CHERRY

Summary

In this JavaScript tutorial, we learned about replace() method of String: the syntax and few working examples with output and detailed explanation for each example.