In this tutorial, you will learn about the Javascript String replace() method with the help of multiple examples.
The Javascript replace()
method finds some or all matches of a pattern and then it replaced those matches with a replacement. Here the pattern can be any string or regex and the replacement can be any string or function called for each match. Now let’s understand this with an example.
Example
let str = "java is a programming language";
//here we finding the match of java (pattern)
//we replaced the match with javascript(replacement)
let output = str.replace("java","javascript");
console.log(output);
//Output: javascript is a programming language
Also Read :
Javascript replace() Syntax
Here is the syntax of the javascript string replace()
method:
string.replace(pattern, replacement)
Here string
is any string on which you want to use replace() method.
Javascript replace() Parameters
Javascript replace() Parameters
pattern – it can be any string or regex which will be used to find matches (Required).
replacement – it can be any string or function. If it is a string it will replace the match found by a pattern else if it is a function it will be called for every match and its return value will replace the match (Required).
Example 1: Using Javascript replace() to Replace First Occurrence
let str = "java is a programming language. Java is fun";
//using string as a pattern
let pattern1 = "java"
let output = str.replace(pattern1, "javascript");
console.log(output);
//Output: javascript is a programming language. Java is fun
//using regex as a pattern
let pattern2 = /java/;
let output2 = str.replace(pattern2, "javascript");
console.log(output2);
//Output: javascript is a programming language. Java is fun
Output
javascript is a programming language
javascript is a programming language
Here you can see we used string and regex as a pattern to find the match “java
“ and then we replaced the match with “javascript
”.
Example 2: Using Javascript replace() to Replace all occurrences
To replace all occurrences of a match we need to use a regex with the “g
” modifier. Here we are using “g
” modifier to perform the global matching. For Example, we will use /java/g
instead of /java/
.
let str = "java is a programming language. java is fun";
let pattern = /java/g;
let output = str.replace(pattern, "javascript");
console.log(output);
//Output: javascript is a programming language. javascript is fun
Output
javascript is a programming language. javascript is fun
Example 3: Using Javascript replace() Without Considering Uppercase/Lowercase
Javascript replace()
method is case-sensitive. For example, if you are finding match for the word “java
” which is in lowercase, then exactly the same occurrence will be considered the match.
To perform case-insensitive matching we need to use the regex with “i
” modifier. For example, we will use /java/g
instead of /java/
.
let str = "JAVA and java";
//performing case-insensitive matching
//to replace the first occurrence
let pattern = /java/i;
let output = str.replace(pattern, "javascript");
console.log(output);
//Output: javascript and java
//performing case-insensitive matching
//with global modifier
//to replace all occurrences
let pattern2 = /java/i;
let output2 = str.replace(pattern, "javascript");
console.log(output2);
//Output: javascript and javascript
Output
javascript and java
javascript and java
Example 4: Using a replacement function
Instead of using a string as the replacement, you can also pass a function in the second parameter of the replace()
method.
let str = "java is a programming language. java is fun";
//function to convert string to uppercase
function capitalize(match){
return match.toUpperCase();
}
let pattern = /java/g;
let output = str.replace(pattern, capitalize);
console.log(output);
//Output: JAVA is a programming language. JAVA is fun
Output
JAVA is a programming language. JAVA is fun
Here you can see we passed capitalize function in the second parameter of replace()
method. It will be called with each match and in replacement, it will return occurrences in uppercase.