AS3正则表达式返回null [英] AS3 RegEx returns null

查看:205
本文介绍了AS3正则表达式返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以解释为什么code以下的痕迹空当时间轴上?

  VAR cleanRegExp:正则表达式= / ^ [A-ZA-Z0-9] +(\ B | \ /)/;
VAR海峡:字符串=/ num83r5 /和/字母/ 4 / A /;
跟踪(str.match(cleanRegExp.toString()));
 

我读过的文档,所以我pretty的确保我正确地宣布正则表达式和 String.match()应仅返回空当没有模式传入,否则应与0+元素的数组。我怀疑是写的不好EX pression,但肯定应该仍然会返回一个空数组?

编辑:这两个跟踪不匹配,而不是5或0,这取决于EX pression是正确的:

  VAR cleanRegExp:正则表达式= / ^ [A-ZA-Z0-9] +(\ B | \ /)/;
VAR海峡:字符串=/ num83r5 /和/字母/ 4 / A /;
VAR RES:阵列= str.match(cleanRegExp);
跟踪((RES == NULL)不匹配:?res.length);
 

  VAR cleanRegExp:正则表达式= / ^ [A-ZA-Z0-9] +(\ B | \ /)/;
VAR海峡:字符串=/ num83r5 /和/字母/ 4 / A /;
VAR RES:对象= cleanRegExp.exec(STR);
跟踪((RES == NULL)不匹配:RES [0]);
 

解决方案

更新

如果你打算在闪光灯与正则表达式的工作,这个工具是一个必须具备的:

http://gskinner.com/RegExr/
http://gskinner.com/RegExr/desktop/

原来的答案

不要使用的toString(),你那么做文字搜索,其中包括增加你所有的正则表达式的格式,包括标志的。做:

  str.match(cleanRegExp);
 

其实正确的方法是将引用返回的对象,像这样:

  VAR结果:阵列= str.match(cleanRegExp);

如果(结果!= NULL){
     //我们有一场比赛!
}
 

Can anyone explain why the code below traces null when on the timeline?

var cleanRegExp:RegExp = /^[a-zA-Z0-9]+(\b|\/)/;
var str:String = "/num83r5/and/letters/4/A/";
trace(str.match(cleanRegExp.toString()));

I've read the documentation, so I'm pretty sure that I'm declaring the RegEx correctly and that String.match() should only return null when no pattern is passed in, otherwise it should be an array with 0+ elements. I suspected a badly written expression, but surely that should still return an empty array?

EDIT: Both these trace "no matches" instead of either 5 or 0, depending on the expression being correct:

var cleanRegExp:RegExp = /^[a-zA-Z0-9]+(\b|\/)/;
var str:String = "/num83r5/and/letters/4/A/";
var res:Array = str.match(cleanRegExp);
trace((res == null) ? "no matches" : res.length);

And:

var cleanRegExp:RegExp = /^[a-zA-Z0-9]+(\b|\/)/;
var str:String = "/num83r5/and/letters/4/A/";
var res:Object = cleanRegExp.exec(str);
trace((res == null) ? "no matches" : res[0]);

解决方案

UPDATE

If you're going to work in flash with regex, this tool is a must-have:

http://gskinner.com/RegExr/
http://gskinner.com/RegExr/desktop/

ORIGINAL ANSWER

Don't use toString(), you're then doing a literal search, which will include the addition of all of your regex formatting, including flags. Do:

str.match(cleanRegExp);

In fact the proper method is to reference the returned object like so:

var results:Array = str.match(cleanRegExp);

if(results != null){
     //We have a match!
}

这篇关于AS3正则表达式返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆