Skip to content

Remove all special characters from a string in JavaScript

To remove all special characters from a string in JavaScript, you replace function and a regex to identify all special characters and replace them globally in a string.

Advertisements
let str = "!@#$%^&*()Hello~><?':"
str = str.replace(/[^\w\s]/gi, '');
console.log(str); //Hello
See also  Trim white spaces at the end of the string in JavaScript
, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

Leave a Reply

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.