String.prototype.replaceAll
Returns a new string with all matches of a pattern replaced by a replacement
Syntax
string.replaceAll(pattern, replacement)Parameters
| Parameter | Type | Description |
|---|---|---|
| pattern | string | RegExp | Pattern to search for (RegExp must have global flag) |
| replacement | string | Function | Replacement string or function |
Return Value
A new string with all replacements made
Examples
const str = 'aabbcc';
console.log(str.replaceAll('b', 'x')); // 'aaxxcc'const template = 'Hello {name}, welcome to {place}!';
const result = template
.replaceAll('{name}', 'Alice')
.replaceAll('{place}', 'Wonderland');
console.log(result); // 'Hello Alice, welcome to Wonderland!'const csv = '1,2,3,4,5';
console.log(csv.replaceAll(',', ' | ')); // '1 | 2 | 3 | 4 | 5'Understanding String.prototype.replaceAll
The String.prototype.replaceAll method in JavaScript returns a new string with all matches of a pattern replaced by a replacement. It belongs to the String object and is one of the most widely used methods for working with string values in modern JavaScript and TypeScript applications.
The method signature is string.replaceAll(pattern, replacement). It accepts 2 parameters: pattern, replacement. When called, it returns a new string with all replacements made. Understanding when and how to use replaceAll() helps you write more expressive, readable code.
Common use cases for String.prototype.replaceAll include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like string-replace, string-match, string-split, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for String.prototype.replaceAll is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.
Browser Compatibility
Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.
Related Methods
String.prototype.replaceReturns a new string with one or all matches of a pattern replaced by a replacement
String.prototype.matchRetrieves the result of matching a string against a regular expression
String.prototype.splitDivides a string into an ordered list of substrings and returns them as an array
More String Methods
Other methods in the String object
Related Tools
More String Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.