Remove Special Characters (like !, %, &, #) from String | Javascript

No, this post is not about dieting or anything like that… but to remove Special Kharacters. To do this, I was going to use a for loop in javascript that checks each character and if it is not between ‘A’ and ‘Z’, the character will be removed. Until I found this code lying around on the net which makes removing special characters very simple…

function removeSpecialChars(strVal)
{
strVal = strVal.replace(/[^a-zA-Z 0-9]+/g,”);
}

If you have a string like “Clive *& % $ £ !! ><>< Ciapp*&^%ara”, the result would be:

Clive Ciappara

Hope this helped someone. If you have any other methods using Regex statements please feel free to share them. Happy String Cleaning 😉

No Responses

Leave a Reply

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