简体   繁体   中英

Actionscript 3 filter using regex

I am trying to make a filter (user types in a string and I check the string against my list of names and only display names containing that string). I am having a lot of trouble with the regex and am either matching the whole string or pretty much none of the string.

The idea is:

var r:RegExp = new RegExp(filterRequest, "regex");
list.name.match(r);

I have tried:

var r:RegExp = new RegExp(filterRequest, "i"); //matches a-z and A-Z but no special ()*&^%$#@! characters

I have tried a combination of flags and escaping characters but none are working correctly. Any suggestions are appreciated - I also wouldn't mind a better explanation than the poor AS3 documentation . I am also reading this and have read this . Here is more character documentation in as3.

The biggest problem with RegExp(filterRequest, "i"); is that it returns matches for () together where a string may be hello (text) but not ( or ) individually. It also matches ()* , $ , ^ , \\( , | , and . but many of the names do not use any of these characters.

Pattern Edit(s):

"\S\s" // any character except white space and only white space (every character?)

I replaced each special character with an escaped version of itself to fix my problem. I did not escape . because it is useful in a filter to use as a wildcard any character match:

            filterRequest= filterRequest.replace(/\\/g, "\\\\");
            filterRequest= filterRequest.replace(/\(/g, "\\(");
            filterRequest= filterRequest.replace(/\)/g, "\\)");
            filterRequest= filterRequest.replace(/\^/g, "\\^");
            filterRequest= filterRequest.replace(/\$/g, "\\$");
            filterRequest= filterRequest.replace(/\|/g, "\\|");
            filterRequest= filterRequest.replace(/\?/g, "\\|?");
            filterRequest= filterRequest.replace(/\*/g, "\\*");
            filterRequest= filterRequest.replace(/\+/g, "\\+");
            filterRequest= filterRequest.replace(/\[/g, "\\[");
            filterRequest= filterRequest.replace(/\{/g, "\\{");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM