Remove special characters using a Regex in JavaScript


Imagine you have some strings and as a requirement of one your clients, you need to clean the strings by deleting all the special characters, so here are two regular expressions that could help you to start your cleaning, of course, the second one is more personalizable, but, be my guest and choose the one you want or need:

var sourceString = "¿Sabes qué es >> te esperamos MAÑANA martes 12 de JUNIO 9.30am en";
// Example with \w
var resultingString = sourceString.replace(/[^\w\s]/gi, '');

// Example listing the specific chars we want to delete
var r = sourceString.replace(/[`~!@#$%^&*()_|+\-=÷¿?;:'",.<>\{\}\[\]\\\/]/gi, '');


One comment on “Remove special characters using a Regex in JavaScript”

Leave a Reply

Your email address will not be published. Required fields are marked *