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
- Replace multiple characters in string in single replace() call
- Replace multiple characters in string using split() and join()
- Replace multiple characters in string using custom function
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:-
Frequently Asked:
- Javascript: Remove Parenthesis from String
- Javascript: replace multiple characters in string
- Javascript: String remove special characters
- JavaScript Remove Zeros from a String
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:
- Javascript: Replace special characters in a string
- Javascript: Replace all occurrences of string (4 ways)
- Javascript: String replace all spaces with underscores (4 ways)
- Javascript: Check if string is url
We hope this article helped you to replace multiple characters in a javascript string. Good Luck !!!