How to remove all special characters using Javascript?

Hello, I want to remove all special characters using javascript, so which is the best method to do it? For example, I have a string "#number 1 \ number 2", how can I get output as 

number 1 number 2

using Javascript. Basically remove all special characters from strings like "#, \, @, $, %,*" etc.

Asked by:- bhanu
: 3602 At:- 12/31/2020 2:55:54 PM
Javascript remove special characters javascript

1 Answers
profileImage Answered by:- vikas_jk

You can use the below Regex to remove all special characters

var YourString="#Check !@test"
var AfterRemovingSpecialCharacters= YourString.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, '');
//output: Check test


You can also try the simpler version

var Output2= YourString.replace(/[^a-zA-Z ]/g,'');
//output: Check test

If you need fiddle link:

Hope this helps.

You can also check similar questions:

How to remove square brackets from string using javascript?

How to remove quotation marks (double quotes) from string using jQuery or Javascript?

How to remove or replace all special characters from a string in C#?

At:- 12/31/2020 3:14:41 PM
Excellent, thanks for quick answer and fiddle link. 0
By : bhanu - at :- 12/31/2020 3:21:49 PM

Login/Register to answer
Register directly by posting answer/details

Full Name *

Email *

By posting your answer you agree on privacy policy & terms of use