Liron Harel
Liron Harel

Reputation: 11247

Remove Characters from a String that doesn't comply with the Regex Rule

I want to know how can I remove characters that doesn't fall into the ones allowed in my Regex expressions.

I use this regular expression which allows all charactes from any language, space and apostrophe, but all other characters are not allowed.

/[\u00C0-\u1FFF\u2C00-\uD7FF\w \']/

I want to create a jquery function that remove all the characters in an text input field that doesn't comply to that rule. So for example: the dagger character '‡' is not allowed and therefore should be replaced with an empty string '' (aka removed from the string).

Upvotes: 2

Views: 493

Answers (1)

adeneo
adeneo

Reputation: 318202

There's no need for jQuery, javascript already has a String.replace() method:

str = str.replace(/[^\u00C0-\u1FFF\u2C00-\uD7FF\w \']/g, '');

or just

str = str.replace(/\W/g, '');

Upvotes: 1

Related Questions