The Wayback Machine - https://web.archive.org/web/20230329142624/https://www.geeksforgeeks.org/javascript-string-replace-method/amp/

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: 

Return Values: It returns a new string with replaced items.

Below is an example of the string.replace() Method. 

Example 1: 




<script>
    var string = 'GeeksForGeeks'
    var newstring = string.replace('GeeksForGeeks', 'GfG'); 
    console.log(newstring); 
</script>

Output: 

GfG

Example 2: Here the contents of the string GeeksForGeeks will be replaced with gfg. 






<script>
    // Assigning a string
    var string = 'GeeksForGeeks is a CS portal';
      
    // Calling replace() method
    var newstring = string.replace(/GeeksForGeeks/, 'gfg');
      
    // Printing replaced string
    console.log(newstring);  
</script>

Output: 

gfg is a CS portal

Example 3: 




<script>
    // Taking a regular expression
    var re = /GeeksForGeeks/;
      
    // Taking a string as input
    var string = 'GeeksForGeeks is a CS portal';
      
    // Calling replace() method to replace
    // GeeksForGeeks from string with gfg
    var newstring = string.replace(re, 'gfg');
      
    // Printing new string with replaced items
    console.log(newstring);
</script>

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.




<script>
    // Assigning a string
    var string = 'GeeksForGeeks is a CS portal.'+
                  'In GeeksForGeeks we can learn multiple languages.'+
                  'geeksForGeeks is a great place.';
      
    // Calling replace() method
    var newstring = string.replace(/GeeksForGeeks/g, 'Gfg');
      
    // Printing replaced string
    console.log(newstring);
</script>

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.

The below example shows the method to replace all the similar words irrespective of their case.

Example 5:




<script>
    // Assigning a string
    var string = 'GeeksForGeeks is a CS portal.'+
                  'In GeeksForGeeks we can learn multiple languages.'+
                  'geeksForGeeks is a great place.';
      
    // Calling replace() method
    var newstring = string.replace(/GeeksForGeeks/gi, 'Gfg');
      
    // Printing replaced string
    console.log(newstring);
</script>

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: 

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.


Article Tags :