JavaScript Program to Replace Characters of a String

Given a String the task is to replace characters of String with a given String in JavaScript.

Examples:

Input: Str = Welcome to GfG
strRepl = GfG
newStr = Beginner
Output: Welcome to Beginner

Table of Content

  • Using map() & split() method to replace characters from String
  • Using String replace() Method to replace characters from String
  • Using Regular Expression to replace characters from String
  • Using split and join Method to Replace Characters from a String

Using map() & split() method to replace characters from String

In this approach, split(‘ ‘) is used to split the string into an array of words. Then, map() is used to iterate over each word in the array. If the word is equal to oldWord, it is replaced with newWord; otherwise, the word remains unchanged. Finally, join(‘ ‘) is used to join the array back into a string

Example: Here we will split the given string “Hello user, welcome to w3wiki” and replace the “Hello” with “Hi”.

Javascript
const str = "Hello user, welcome to w3wiki";
oldWord='Hello';
newWord='Hi'
const replacedString=str.split(' ').map(word => word === oldWord ? newWord : word).join(' ');
console.log(replacedString);

Output
Hi user, welcome to w3wiki

Using String replace() Method to replace characters from String

JavaScript 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: Here we will replace the “GfG” with “Beginner” on the given string “Welocome to GfG”.

Javascript
// JavaScript program to Replace
// Characters of a string
const str = 'Welcome to GfG';
const replStr = 'GfG';
const newStr = 'Beginner';

const updatedStr = str.replace(replStr, newStr);

console.log(updatedStr);

Output
Welcome to Beginner

Using Regular Expression to replace characters from String

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

Example: In this example we will replace “Welcome” with “Hello” in the given string “Welcome w3wiki, Welcome Beginner”.

Javascript
const str = 'Welcome w3wiki, Welcome Beginner';
const searchString ="Welcome";
const replacementString ="Hello";

let regex = new RegExp(searchString, 'g');

let replacedString = 
    str.replace(regex, replacementString);

console.log(replacedString);

Output
Hello w3wiki, Hello Beginner

Using split and join Method to Replace Characters from a String

In this approach, the split method is used to divide the string into an array of substrings based on the substring we want to replace. Then, the join method is used to combine the array back into a string, with the new substring replacing the old one.

Example: Here we will replace “GfG” with “Beginner” in the given string “Welcome to GfG”.

JavaScript
const str = 'Welcome to GfG';
const oldStr = 'GfG';
const newStr = 'Beginner';

const updatedStr = str.split(oldStr).join(newStr);

console.log(updatedStr);

Output
Welcome to Beginner