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.
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#?
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly