简体   繁体   English

使用正则表达式的ActionScript 3过滤器

[英]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 . 任何建议都值得赞赏-我也不会介意比糟糕的AS3 文档更好的解释。 I am also reading this and have read this . 我也在读这本书,并且已经读过 Here is more character documentation in as3. 这是as3中的更多字符文档

The biggest problem with RegExp(filterRequest, "i"); RegExp(filterRequest, "i");的最大问题RegExp(filterRequest, "i"); is that it returns matches for () together where a string may be hello (text) but not ( or ) individually. 是它会返回()匹配项,其中字符串可能是hello (text)但不是()字符串。 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, "\\{");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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