JavaScript string replace() Method
The string.replace() is an inbuilt method in JavaScript that is used to replace a part of the given string with another string or a regular expression. The original string will remain unchanged.
Syntax:
str.replace(value1, value2);
Parameters:
- value1: is the regular expression that is to be replaced
- value2: is a string that will replace the content of the given string.
Return Values:
It returns a new string with replaced items.
Example 1: Below is an example of the string.replace() Method.
javascript
let string = 'GeeksForGeeks';let newstring = string.replace('GeeksForGeeks', 'GfG');console.log(newstring); |
Output:
GfG
Example 2: Here the contents of the string GeeksForGeeks will be replaced with gfg.
javascript
// Assigning a stringlet string = 'GeeksForGeeks is a CS portal'; // Calling replace() methodlet newstring = string.replace(/GeeksForGeeks/, 'gfg');// Printing replaced stringconsole.log(newstring); |
Output:
gfg is a CS portal
Example 3: Below is an example of the string.replace() Method.
javascript
// Taking a regular expressionlet re = /GeeksForGeeks/;// Taking a string as inputlet string = 'GeeksForGeeks is a CS portal';// Calling replace() method to replace// GeeksForGeeks from string with gfglet newstring = string.replace(re, 'gfg');// Printing new string with replaced itemsconsole.log(newstring); |
Output:
gfg is a CS portal
We can also replace the same words at multiple places in a string. It is known as a global replacement.
Example 4: This example explains replacing of various similar words in a string.
Javascript
// Assigning a stringlet string = 'GeeksForGeeks is a CS portal.' + 'In GeeksForGeeks we can learn multiple languages.' + 'geeksForGeeks is a great place.';// Calling replace() methodlet newstring = string.replace(/GeeksForGeeks/g, 'Gfg');// Printing replaced stringconsole.log(newstring); |
Output:
"Gfg is a CS portal.In Gfg we can learn multiple languages.geeksForGeeks is a great place."
Here in this example, we can see that only the ‘GeeksForGeeks’ keywords having the first letter covered are replaced.
Example 5: below example shows the method to replace all the similar words irrespective of their case.
Javascript
// Assigning a stringlet string = 'GeeksForGeeks is a CS portal.' + 'In GeeksForGeeks we can learn multiple languages.' + 'geeksForGeeks is a great place.';// Calling replace() methodlet newstring = string.replace(/GeeksForGeeks/gi, 'Gfg');// Printing replaced stringconsole.log(newstring); |
Output:
"Gfg is a CS portal.In Gfg we can learn multiple languages.Gfg is a great place."
We have a complete list of Javascript string methods, to check those please go through this Javascript String Complete reference article.
Supported Browsers:
- Google Chrome 1 and above
- Edge 12 and above
- Firefox 1 and above
- Internet Explorer 5.5 and above
- Opera 4 and above
- Safari 1 and above
We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.

