Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing invalid characters in JavaScript

Tags:

Can someone provide a regular expression to search and replace illegal characters found

Example, removing �

I am not sure how many types of 'illegal' characters exist but I think this will be a good start.

Many thanks

edit - I have no control over the data, we're trying to create a catch for the potentially bad data we're receiving.

like image 971
Chris Avatar asked Oct 05 '12 21:10

Chris


People also ask

How do I remove a character from a string in JavaScript?

Using a replace() method with a regular expression To remove a character from a string, use string replace() and regular expression. This combination is used to remove all occurrences of the particular character, unlike the previous function. A regular expression is used instead of a string along with global property.

How do you restrict special characters in a textbox using regular expression?

var nospecial=/^[^* | \ " : < > [ ] { } ` \ ( ) '' ; @ & $]+$/; if(address. match(nospecial)){ alert('Special characters like * | \ " : < > [ ] { } ` \ ( ) \'\' ; @ & $ are not allowed'); return false; but it is not working.


1 Answers

Invalid characters get converted to 0xFFFD on parsing, so any invalid character codes would get replaced with:

myString = myString.replace(/\uFFFD/g, '') 

You can get all types of invalid sorts of chars here

like image 140
saml Avatar answered Sep 27 '22 21:09

saml