2010-04-29 68 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

相關問題