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 😉

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s