Opens in a new tab
A picture of Jeff Brigman
I CODE TODAY FOR
A BETTER TOMORROW

Glossary Term

String.prototype.replaceAll()

2 tags 1 category

The replaceAll() method of String values returns a new string with all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp, and the replacement can be a string or a function to be called for each match. The original string is left unchanged.

This method does not mutate the string value it’s called on. It returns a new string.

Unlike replace(), this method replaces all occurrences of a string, not just the first one. While it is also possible to use replace() with a global regex dynamically constructed with RegExp() to replace all instances of a string, this can have unintended consequences if the string contains special characters that have meaning in regular expressions (which might happen if the replacement string comes from user input). While you can mitigate this case using RegExp.escape() to make the regular expression string into a literal pattern, it is simpler to pass the string to replaceAll() directly, without converting it to a regex.

Syntax example: replaceAll(pattern, replacement)

Term Connections

Explore related terms connected by shared tags, shared categories, and relationship phrases. This map loads only when it comes into view.

Loading term connections…