Sunday, September 22, 2024
Google search engine
HomeLanguagesJavascriptHow to replace all occurrences of a string in JavaScript ?

How to replace all occurrences of a string in JavaScript ?

In this article, we will see how to replace all occurrences of a string in JavaScript. We can replace all the occurrences with another string by using replace built-in method. We can also replace it by using various methods.

These are the following methods to replace all occurrences of a string in JavaScript:

Method 1: Using string.replace() method

The string.replace() method is used to replace a part of the given string with another string or a regular expression. The original string will remain unchanged.

Example:

Javascript




// Origin String
const str = 'Welcome neveropen, Welcome neveropen';
 
// Replace all occurrence of Welcome with Hello
const newString = str.replace(/Welcome/gi, 'Hello');
 
// Display the result
console.log(newString);


Output

Hello neveropen, Hello neveropen

Method 2: Using String split() and join() Method

We can split the strings of text with the JavaScript split method and join strings using the replace characters with the join method. 

Example:

Javascript




// Original String
const str = 'Welcome neveropen, Welcome neveropen';
 
// Replace all occurrence of Welcome with Hello
const newString = str.split('Welcome').join('Hello');
 
// Display the result
console.log(newString);


Output

Hello neveropen, Hello neveropen

Method 3: Using replaceAll() Method

The replaceAll() method is used to replace all the matches of a string with a specified string or a regular expression. The original string is left unchanged after this operation.

Example:

Javascript




// Original String
const str = 'Welcome neveropen, Welcome neveropen';
 
// Replace all occurrences of Welcome with Hello
const newString = str.replaceAll("Welcome", "Hello");
 
// Display the result
console.log(newString);


Output

Hello neveropen, Hello neveropen

Method 4: Using regular expression

To replace all occurrences of a string in JavaScript using a regular expression, we can use the regular expression with the global (g) Flag.

Javascript




const str = 'Welcome neveropen, Welcome neveropen';
const searchString ="Welcome";
const replacementString ="Hello";
 
let regex = new RegExp(searchString, 'g');
let replacedString = str.replace(regex, replacementString);
console.log(replacedString);


Output

Hello neveropen, Hello neveropen

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen 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!

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments