Saturday, November 16, 2024
Google search engine
HomeLanguagesJavascriptJavaScript string replace() Method

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.

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

Example 1: 

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 string
let string = 'GeeksForGeeks is a CS portal';
     
// Calling replace() method
let newstring = string.replace(/GeeksForGeeks/, 'gfg');
  
// Printing replaced string
console.log(newstring);


Output: 

gfg is a CS portal

Example 3: 

javascript




// Taking a regular expression
let re = /GeeksForGeeks/;
  
// Taking a string as input
let string = 'GeeksForGeeks is a CS portal';
  
// Calling replace() method to replace
// GeeksForGeeks from string with gfg
let newstring = string.replace(re, 'gfg');
  
// Printing new string with replaced items
console.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 string
let string = 'GeeksForGeeks is a CS portal.' +
    'In GeeksForGeeks we can learn multiple languages.' +
    'neveropenForGeeks is a great place.';
  
// Calling replace() method
let newstring = string.replace(/GeeksForGeeks/g, 'Gfg');
  
// Printing replaced string
console.log(newstring);


Output:

"Gfg is a CS portal.In Gfg we can learn multiple languages.neveropenForGeeks 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




// Assigning a string
let string = 'GeeksForGeeks is a CS portal.' +
    'In GeeksForGeeks we can learn multiple languages.' +
    'neveropenForGeeks is a great place.';
  
// Calling replace() method
let newstring = string.replace(/GeeksForGeeks/gi, 'Gfg');
  
// Printing replaced string
console.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.

RELATED ARTICLES

Most Popular

Recent Comments