Javascript: replace multiple characters in string

This article will discuss replacing multiple characters in a javascript string using different methods and example illustrations.

Table of Contents:-

Replace multiple characters in string by chaining replace() function

Example:-

Replace ‘^’ (circumflex), ‘$’ (dollar), ‘_’ (underscore) in the string “Javascript^ is$ the most popular _language” with ‘(nothing)’

Code:-

let dummyString = 'Javascript^ is$ the most popular _language';
newString = dummyString.replace('_','').replace('^', '').replace('$','');
console.log(newString);

Output:-

Javascript is the most popular language

Example:-

Replace ‘^’ with ”, ‘$’ with ‘+’ and ‘_’ with ‘:’ in the string “Javascript^ is$ the most popular _language”

Code:-

let dummyString = 'Javascript^ is$ the most popular _language';
newString = dummyString.replace('_',':').replace('^', '').replace('$','+');
console.log(newString);

Output:-

Javascript is+ the most popular :language

Replace multiple characters in single replace() call

Example:-

Replace ‘^’ (circumflex), ‘$’ (dollar), ‘_’ (underscore) in the string “Javascript^ is$ the most popular _language” with ‘(nothing)

Code:-

let dummyString = 'Javascript^ is$ the most popular _language';
newString = dummyString.replace(/$|_|\^/gi,''); 
console.log(newString);

Output:-

Javascript is the most popular language

Example:-

Replace ‘^’ with ”, ‘$’ with ‘+’ and ‘_’ with ‘:’ in the string “Javascript^ is$ the most popular _language

Code:-

let dummyString = 'Javascript^ is$ the most popular _language';
newString = dummyString.replace(/[_^$]/g, charactersToReplace => ({'^': '', '_': ':', '$' : '+' })[charactersToReplace]);
console.log(newString);

Output:-

Javascript is+ the most popular :language

Replace multiple characters in string using split() and join()

Example:-

Replace ‘^’ with ”, ‘$’ with ‘+’ and ‘_’ with ‘:’ in the string “Javascript^ is$ the most popular _language

Code:-

let dummyString = "Javascript^ is$ the most popular _language";
let newstring1 = dummyString.split('^').join('');
let newstring2 = newstring1.split('_').join(':');
let newstring3 = newstring2.split('$').join('+');
console.log(newstring3);

Output:-

Javascript is+ the most popular :language

Replace multiple characters in string using custom function

Example:-

Replace ‘^’ with ”, ‘$’ with ‘+’ and ‘_’ with ‘:’ in the string “Javascript^ is$ the most popular _language”

Function:-

function replaceMulCharInString(_string, _charToReplace, _replaceWith)
{
    for (var i = 0; i < _charToReplace.length; i++) {
        _string = _string.replace(new RegExp(_charToReplace[i], 'gi'), _replaceWith[i]);
    }
    return _string;
}

Usage:-

let dummyString = "Javascript^ is$ the most popular _language";
let charToReplace = ['\\^','_','\$'];
let replaceWithChar = ['',':','+'];
newString = replaceMulCharInString(dummyString, charToReplace, replaceWithChar);
console.log(newString);

Output:-

Javascript is+ the most popular :language

Read More:

We hope this article helped you to replace multiple characters in a javascript string. Good Luck !!!

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top