This article discusses replacing all special characters in a javascript string using different methods and examples.

Table of Contents:-

Javascript replace regex special characters in a string using replace()

The javascript replace() method replaces some or all occurrences of a pattern with a replacement(character/string). The pattern can be a character or a string, or regExp.

Syntax:-

replace(regexp, replacement)

Example:-

Replace all occurrences of all special characters with “” (empty) from the string “Javascript is @ a # language, This is : the most %popular _ language.”

let newString = ""
let dummyString = "Javascript is @ a # language, This is : the most %popular _ language."
newString = dummyString.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, '')
console.log(dummyString)
console.log(newString)

Output:-

Javascript is @ a # language, This is : the most %popular _ language.
Javascript is  a  language This is  the most popular  language

Here, in the replace() function, the first argument takes the characters which we want to replace. The second argument is the replacement character.

Note that here ‘,'(comma) and ‘.'(dot) are also removed. In case you want to retain, use:

let newString = ""
let dummyString = "Javascript is @ a # language, This is : the most %popular _ language."
newString = dummyString.replace(/[`~!@#$%^&*()_|+\-=?;:'"<>\{\}\[\]\\\/]/gi, '')
console.log(dummyString)
console.log(newString)

Output:-

Javascript is @ a # language, This is : the most %popular _ language.
Javascript is  a  language, This is  the most popular  language.

Example:-

Replace all occurrences of all special characters with “_” (underscore) from the string “Javascript is @ a # language, This is : the most %popular _ language.”

let dummyString = "Javascript is @ a # language, This is : the most %popular _ language."
newString = dummyString.replace(/[^a-zA-Z0-9][\W]/gi,'_')
console.log(dummyString)
console.log(newString)

Output:-

Javascript is @ a # language, This is : the most %popular _ language.
Javascript is_ a_ language_This is_ the most_popular _language.

Javascript replace special characters in a string using a custom function

In this section, we will be creating a function to replace the special characters in a javascript string. We can use this function to replace all special characters of our choice while retaining the ones we need in the string.

Example:-

Replace all occurrences of all special characters with “” (empty) but retain the ‘,’ (comma) and ‘.’ (dots) from the string “Javascript is @ a # language, This is : the most %popular _ language.”

let dummyString = "Javascript is @ a # language, This is : the most %popular  _language."
function replaceSpecialCharacters(_string) 
    {
    var lowerCase = _string.toLowerCase();
    var upperCase = _string.toUpperCase();
    var replacement = "";
    for(var i=0; i<lowerCase.length; ++i) {
        if(lowerCase[i] != upperCase[i] || lowerCase[i].trim() === '' || lowerCase[i].trim() === "." || 
        lowerCase[i].trim() === ",")
        replacement += _string[i];
    }
    return replacement;
}
let newString = replaceSpecialCharacters(dummyString)
console.log(newString)

Output:-

Javascript is  a  language, This is  the most popular  language.

The function will replace all the special characters with empty where whitespaces, comma, and dots are ignored. The concept is to compare the uppercase and lower case characters, and the characters with the same value for the upper and lower case are special characters, so remove them from the string.

Read More:

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