String replaceAll()
String.prototype.replaceAll() replaces every occurrence of a string or global pattern. It removes the need for repetitive chaining or manual global regular expressions in common cases.
Overview
String.prototype.replaceAll() replaces every occurrence of a string or global pattern. It removes the need for repetitive chaining or manual global regular expressions in common cases.
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.
Related links
Powered by web-features