2010-04-29 37 views

回答

1

如果您想为您在严格意义上的字的存在(作为一个整体的字,一个字不一部分),你可以使用正则表达式。例如,字符串"hat"作为子字符串包含在字符串"I like that movie"中,但不是整个字。使用String::match(pattern:*)方法在字符串中搜索正则表达式。

var str:String = "I like that movie"; 
var t1:RegExp = /hat/;  // `/` is the delimiter 
var t2:String = /\bhat\b/; // `\b` denotes word boundary 
if(str.match(t1) != null) 
    trace("hat is a substring"); 
if(str.match(t2) == null) 
    trace("but hat is not present as whole word"); 
+1

只需要注意,RegExp :: test(string:String)将返回一个布尔值,从而删除与null的比较。例如:if(t1.test(str)){...} – 2012-08-24 21:37:28

相关问题