Syntax
str.replace(pattern, replacement)
str.replaceAll(pattern, replacement)Examples
replace() - First Match Only
The default behavior replaces only the first occurrence.
const text = "cat and cat and cat";
console.log(text.replace("cat", "dog"));
// "dog and cat and cat" - only the first match is replacedreplaceAll() - Every Match
Replacing all occurrences without needing a regex.
const text = "cat and cat and cat";
console.log(text.replaceAll("cat", "dog"));
// "dog and dog and dog"Using a Function as the Replacement
Dynamically computing the replacement value for each match.
const prices = "Item1: $10, Item2: $25";
const discounted = prices.replace(/\$(\d+)/g, (match, amount) => {
return `$${Math.round(amount * 0.9)}`;
});
console.log(discounted); // "Item1: $9, Item2: $23" (rounded)Best practices
- Use replaceAll() for simple string replacements when you want every occurrence changed - it needs no regex or g flag
- Use replace() with a regex and the g flag if you need pattern-based (not literal string) global replacement
- Remember these methods return a new string - the original string is never modified, since strings are immutable
- Use a function as the replacement argument when the new value needs to be computed based on the match itself
At a glance
- Purpose
- Application logic and interaction
- File extension
- .js ยท .mjs
- Runs in
- Browsers and JavaScript runtimes
- Usually used with
- HTML, CSS and Web APIs
Specifications & further reading
Related JavaScript documentation
String Basics: length, charAt(), at()
Strings are immutable sequences of characters - every string method returns a new string rather than modifying the original. The length property gives the character count. charAt() returns the character at a given index, while the newer at() method also supports negative indices to count from the end.slice(), substring(), substr()
These three methods extract a portion of a string as a new string. slice() and substring() are similar, but slice() accepts negative indices (counting from the end) while substring() treats negative values as 0 and swaps arguments if start is greater than end. substr() (deprecated) uses a start index and a length rather than an end index.indexOf(), includes(), startsWith(), endsWith()
These methods search within a string for a substring. indexOf() returns the position of the first match (or -1). includes() returns a simple true/false. startsWith() and endsWith() check specifically whether the string begins or ends with a given substring. All are case-sensitive.split()
split() divides a string into an array of substrings based on a specified separator, which can be a literal string or a regular expression. It is the inverse operation of Array.prototype.join(). An optional second argument limits the number of resulting elements.
Strings are immutable sequences of characters - every string method returns a new string rather than modifying the original. The length property gives the character count. charAt() returns the character at a given index, while the newer at() method also supports negative indices to count from the end.slice(), substring(), substr()
These three methods extract a portion of a string as a new string. slice() and substring() are similar, but slice() accepts negative indices (counting from the end) while substring() treats negative values as 0 and swaps arguments if start is greater than end. substr() (deprecated) uses a start index and a length rather than an end index.indexOf(), includes(), startsWith(), endsWith()
These methods search within a string for a substring. indexOf() returns the position of the first match (or -1). includes() returns a simple true/false. startsWith() and endsWith() check specifically whether the string begins or ends with a given substring. All are case-sensitive.split()
split() divides a string into an array of substrings based on a specified separator, which can be a literal string or a regular expression. It is the inverse operation of Array.prototype.join(). An optional second argument limits the number of resulting elements.