String replaceAll()
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.
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| 85 | 85 | 77 | 13.1 | 85 | 13.4 | |
1+Supported (version) Not supported ※Has note Sub-feature descriptions sourced from MDN Web Docs (CC BY-SA 2.5)
Syntax
JAVASCRIPT
const template = 'Hello {{name}}, welcome to {{name}}'s page!';
template.replaceAll('{{name}}', 'Taro');
// 'Hello Taro, welcome to Taro's page!'
// Traditional method (required regular expressions)
template.replace(/\{\{name\}\}/g, 'Taro'); Live demo
Use cases
-
Template replacement
Swap placeholder tokens in messages, labels, or exported text without writing extra loop logic.
-
Bulk text cleanup
Normalize separators, remove repeated tokens, or redact known phrases across a full string.
Cautions
- String replacement is still exact and case-sensitive unless you normalize input or use a proper regular expression.
- For user-generated HTML or rich text, replacement alone is not sanitization.
Accessibility
- If redaction or replacement changes visible copy, make sure the final wording remains understandable and does not remove essential context.