JavaScript String replaceAll()
Syntax & Examples
String.replaceAll() method
The replaceAll() method of the String class in JavaScript is used to replace all occurrences of a specified pattern with a replacement string or the result of a function.
Syntax of String.replaceAll()
The syntax of String.replaceAll() method is:
replaceAll(pattern, replacement)
This replaceAll() method of String used to replace all occurrences of searchFor using replaceWith. searchFor may be a string or Regular Expression, and replaceWith may be a string or function.
Parameters
Parameter | Optional/Required | Description |
---|---|---|
pattern | required | The pattern to search for. It may be a string or a regular expression. |
replacement | required | The replacement string or function to use when replacing occurrences of the pattern. |
Return Type
String.replaceAll() returns value of type String
.
✐ Examples
1 Using replaceAll() method to replace all occurrences of a substring
In JavaScript, the replaceAll()
method can be used to replace all occurrences of a substring with another string.
For example,
- We define a string variable
str
with the value'hello hello hello'
. - We use the
replaceAll()
method to replace all occurrences of'hello'
with'world'
. - The result is stored in the variable
newStr
. - We log
newStr
to the console using theconsole.log()
method.
JavaScript Program
const str = 'hello hello hello';
const newStr = str.replaceAll('hello', 'world');
console.log(newStr);
Output
world world world
2 Using replaceAll() method with a regular expression
In JavaScript, the replaceAll()
method can use a regular expression as the pattern to replace all occurrences of a specified pattern with a replacement string.
For example,
- We define a string variable
str
with the value'The quick brown fox jumps over the lazy dog'
. - We use the
replaceAll()
method with a regular expression to replace all occurrences of vowels with'*'
. - The result is stored in the variable
newStr
. - We log
newStr
to the console using theconsole.log()
method.
JavaScript Program
const str = 'The quick brown fox jumps over the lazy dog';
const newStr = str.replaceAll(/[aeiou]/ig, '*');
console.log(newStr);
Output
Th* q**ck br*wn f*x j*mps *v*r th* l*zy d*g
3 Using replaceAll() method with a function
In JavaScript, the replaceAll()
method can use a function as the replacement. The function is invoked for each match found, and its return value is used as the replacement.
For example,
- We define a string variable
str
with the value'apple, banana, cherry'
. - We use the
replaceAll()
method with a function to replace each fruit with its uppercase version. - The result is stored in the variable
newStr
. - We log
newStr
to the console using theconsole.log()
method.
JavaScript Program
const str = 'apple, banana, cherry';
const newStr = str.replaceAll(/\b\w+\b/g, function(match) {
return match.toUpperCase();
});
console.log(newStr);
Output
APPLE, BANANA, CHERRY
Summary
In this JavaScript tutorial, we learned about replaceAll() method of String: the syntax and few working examples with output and detailed explanation for each example.