How to use String replace() Method to replace characters from String In Javascript
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 “Geeks” on the given string “Welocome to GfG”.
// JavaScript program to Replace
// Characters of a string
const str = 'Welcome to GfG';
const replStr = 'GfG';
const newStr = 'Geeks';
const updatedStr = str.replace(replStr, newStr);
console.log(updatedStr);
Output
Welcome to Geeks
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 = Geeks
Output: Welcome to Geeks
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