String .replace() / replaceAll()
| Since: | ES1(ECMAScript 1997) |
|---|
Replaces a specified string or pattern within a string with another string. The original string is not modified — a new string is returned.
Syntax
str.replace("search string", "replacement string")
str.replace(/regex/, "replacement string")
// Replaces all matches.
str.replaceAll("search string", "replacement string")
Method List
| Method | Description |
|---|---|
| replace("search string", "replacement string") | Returns a new string with only the first match replaced. |
| replace(/regex/, "replacement string") | Replaces the portion matching the regular expression. Using the g flag replaces all occurrences. |
| replaceAll("search string", "replacement string") | Returns a new string with all matches replaced. |
Sample Code
var str = "item_a is a pending item. A pending item requires review.";
// replace replaces only the first match.
console.log(str.replace("pending", "active")); // Outputs: "item_a is a active item. A pending item requires review."
// replaceAll replaces all matches.
console.log(str.replaceAll("pending", "active")); // Outputs: "item_a is a active item. A active item requires review."
// Using a regex with the g flag also replaces all occurrences.
console.log(str.replace(/pending/g, "active")); // Outputs: "item_a is a active item. A active item requires review."
// Using a regex with the i flag to replace case-insensitively.
var label = "product product PRODUCT";
console.log(label.replace(/product/gi, "item")); // Outputs: "item item item"
// The original string is not modified.
console.log(str); // Outputs: "item_a is a pending item. A pending item requires review."
// Text formatting: Remove hyphens from a phone number.
var phone = "090-1234-5678";
var normalized = phone.replaceAll("-", "");
console.log(normalized); // Outputs '09012345678'.
// Template substitution: Replace placeholders with actual values.
var template = "Hello, {name}. Your point balance is {score}.";
var message = template.replace("{name}", "user1").replace("{score}", "500");
console.log(message); // Outputs 'Hello, user1. Your point balance is 500.'.
item_a is a active item. A pending item requires review. item_a is a active item. A active item requires review. item_a is a active item. A active item requires review. item item item item_a is a pending item. A pending item requires review. 09012345678 Hello, user1. Your point balance is 500.
Notes
string.replace() replaces only the first match. When passing a plain string, note that only the first occurrence is replaced. To replace all occurrences, use string.replaceAll() or pass a regular expression with the g flag to string.replace().
string.replaceAll() is a relatively new method added in ES2021 that replaces all matches at once. The result is equivalent to using a regex with the g flag, but string.replaceAll() is more readable if you are not familiar with regular expressions.
Both methods leave the original string unchanged and return a new string with the replacements applied. To use the result, assign it to a variable or use it directly.
Browser Compatibility
5 ×
4.5 △
4 △
3.5 or earlier ×
3 or earlier ×
Android Browser
37+ ○
4 or earlier ×
Chrome Android
36+ ○
17 or earlier ×
Firefox Android
79+ ○
3 or earlier ×If you find any errors or copyright issues, please contact us.