Vlad
Vlad

Reputation: 2773

About finding chars in text with AS3

I have to validate a textfield in AS3 if there are chars like # $ % @ ~ | {} [] / \ etc. i.e find unnecessary chars and remove them from the textfield.

I use a search AS3 function which works with regular expressions, but I am not good at RegEx so can anyone help me with this? I would like a regular expression so that a search function searches the characters above and if it finds any of them then returns true.

Upvotes: 0

Views: 602

Answers (2)

user1080806
user1080806

Reputation:

There may be a more concise regex, but for the characters you've given, try this.

var pattern:RegExp = /[#$%@~\|{}\[\]\\\/]/

Upvotes: 1

ridgerunner
ridgerunner

Reputation: 34395

The following snippet removes the blacklisted characters you have defined (not recommended):

Option 1: Character class with blacklist.

text = text.replace(/[#$%@~|{}[\]/\\]+/g, '');

But there are a LOT of other control, punctuation and Unicode characters that you probably also want to avoid (e.g. [¥®^«±µ¼½¾] etc.. Instead, it is most likely better to define which chars you wish to allow, and then remove all characters that are NOT the ones to be allowed.

Option 2: Negated character class with whitelist.

Lets say you want to allow all latin letters, numbers, spaces, dots, dashes, underscores and colons (i.e. [A-Za-z0-9 .\-_:]). Here is a snippet that will remove all character that are not in the whitelist:

text = text.replace(/[^A-Za-z0-9 .\-_:]+/g, '');

Upvotes: 1

Related Questions