Javascript: String remove special characters

While working in javascript, we often encounter a general requirement to remove special characters from a javascript string. One such example is to remove special characters from the phone number string. This article will illustrate how to remove special characters from a javascript string using different methods and examples.

Table of Contents:-

Javascript string remove all special characters

Example:-

Remove all special characters from string “23 ,67 -09% 2 Dummy^Address*Text”

Code:-

const dummyString = "23 ,67 -09% 2  Dummy^Address*Text"
let finalString =  dummyString.replace(/[^a-zA-Z0-9]/g, '')
console.log("Original String: "+ dummyString )
console.log("Final String: "+ finalString)

Here in the above code, we are using replace() method with RegExp. The regular expression is passed as the first parameter that states that this replace() method should replace all characters except numbers and alphabets. The second parameter is the replacement, nothing (”) in our case.

Output:-

Original String: 23 ,67 -09% 2  Dummy^Address*Text
Final String: 2367092DummyAddressText

Javascript string remove specific special characters

Example:-

Remove special characters from string “23 ,67 -09% 2 !@Dummy^Address* Text” but keep comma (,) and hyphen (-)

Code:-

const dummyString = "23 ,67 -09% 2  !@Dummy^Address* Text"
let finalString =  dummyString.replace(/[&\/\\#^+()$~%.'":*?<>{}!@]/g, '') 
console.log("Original String: "+ dummyString )
console.log("Final String: "+ finalString)

Here again, in the above code, we are using replace() method with RegExp. The regular expression is passed as the first parameter. This regular expression defines removing a lot of special characters. Here in this regular expression we will specify only those special characters which we want to remove. The second parameter is the replacement which states to replace the special characters with nothing (”) in our case.

Output:-

Original String: 23 ,67 -09% 2  !@Dummy^Address* Text
Final String: 23 ,67 -09 2  DummyAddress Text

As we can see in the output, comma (‘) and hyphen (-) are still there.

Javascript string remove special characters except numbers

Example:-

Remove special characters from phone number string “443 ,67+ -09% 2 97754”

Code:-

const dummyString =  "443 ,67+ -09% 2  97754"
let finalString =  dummyString.replace(/[^0-9]/g, '') 
console.log("Original String: "+ dummyString )
console.log("Final String: "+ finalString)

Here, in the above replace() method the regular expression states to replace all characters except the numbers.

Output:-

Original String: 443 ,67+ -09% 2  97754
Final String: 4436709297754

Javascript string remove special characters except space and dot

The same replace() method will be used in the below code to remove the special characters but keep the spaces (” “) and dot(“.”). The simple way is to replace everything except numbers, alphabets, spaces, and dots.

Example:-

Remove special characters from the string “Javascript #*is a #popular language.” except spaces and dot

Code:-

const dummyString =  "Javascript #*is a #popular language."
let finalString =  dummyString.replace(/[^0-9a-zA-Z. ]/g, '') 
console.log("Original String: "+ dummyString )
console.log("Final String: "+ finalString)

Output:-

Original String: Javascript #*is a #popular language.
Final String: Javascript is a popular language.

Javascript string remove escape characters

Example:-

Remove escape characters from the string “Javascript-\Is-\\Popular\\\\\”

Code:-

const dummyString = "Javascript-\\Is-\\\\Popular\\\\\\\\\\"
let finalString =  dummyString.replace(/\\/gi, '') 
console.log("Original String: "+ dummyString )
console.log("Final String: "+ finalString)

Output:-

Original String: Javascript-\Is-\\Popular\\\\\
Final String: Javascript-Is-Popular

Read More:

I hope this article helped you to remove special characters from 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