.replaceAll()

Anonymous contributor's avatar
Anonymous contributor
Anonymous contributor's avatar
Anonymous contributor
Published Oct 9, 2023
Contribute to Docs

The .replaceAll() method returns a new string by replacing all the matches in a string of a given search value with a given replacement value.

Syntax

string.replaceAll(searchValue, replacementValue);

The searchValue can be either a string or RegExp.

Example

Replace all the occurrences of "scream" with "laugh":

const sentence = `I scream, you scream, we all scream for ice cream.`;
console.log(sentence.replaceAll('scream', 'laugh'));

This example results in the following output:

I laugh, you laugh, we all laugh for ice cream.

All contributors

Looking to contribute?

Learn JavaScript on Codecademy