Skip to content
Related Articles
Open in App
Not now

Related Articles

JavaScript string.replace() Method

Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 28 Nov, 2022
Improve Article
Save Article

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.

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

Example 1: 

javascript




<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. 

javascript




<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: 

javascript




<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.

Javascript




<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:

Javascript




<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: 

  • 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.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!