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
0
: 256 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, '');
console.log(AfterRemovingSpecialCharacters);
//output: Check test

OR

You can also try the simpler version

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

If you need fiddle link: https://jsfiddle.net/ae58rhq4/

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#?

1
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
Or
Register directly by posting answer/details

Full Name *

Email *




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

Subscribe Now

Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly