2012-08-07 67 views
0

我在尋找一個正則表達式來解析a =和&之間的部分以獲得第一個url變量。Flex正則表達式找到url中兩個字符之間的匹配

URL示例:

http://vandooren.be?v=123456&test=123 

我需要從字符串123456。

我最後的嘗試是

var pattern:RegExp = /\=|\&/; 
var result:Array = pattern.exec(dg.selectedItem.link[0]); 
trace(result.index, " - ", result); 

但我仍然得到錯誤。

+0

有一個URI/URL(分析器)類?否則,你可以在'?'處取出最後一個標記,然後沿'&'分開,然後對於每個標記,找到以名稱開頭並沿着= =分開的標記。 – nhahtdh 2012-08-07 06:55:59

+0

是的,我會從URL這個變量得到這麼愚蠢我沒有想到這個 – 2012-08-07 07:08:31

回答

1

請嘗試以下代碼。

var myPattern:RegExp = /(?<==).+(?=&)/; 
var str:String = "http://vandooren.be?v=123456&test=123"; 
var result:Array = myPattern.exec(str); 
trace(result[0]); //123456 

var myPattern:RegExp = /(?<==).+(?=&)/; 
var str:String = "youtube.com/watch?v=nCgQDjiotG0&feature=youtube_gdata"; 
var result:Array = myPattern.exec(str); 
trace(result[0]); //nCgQDjiotG0 

Assertions

foo(?=bar) Lookahead assertion. The pattern foo will only match if followed by a match of pattern bar. 
foo(?!bar) Negative lookahead assertion. The pattern foo will only match if not followed by a match of pattern bar. 
(?<=foo)bar Lookbehind assertion. The pattern bar will only match if preceeded by a match of pattern foo. 
(?<!foo)bar Negative lookbehind assertion. The pattern bar will only match if not preceeded by a match of pattern foo. 
+0

它的作品在該網址,但當我得到這樣的網址'http://www.youtube.com/watch?v=nCgQDjiotG0&feature=youtube_gdata'它返回null – 2012-08-07 07:29:20

+0

我的回答編輯。它適用於以下網址'youtube.com/watch?v=nCgQDjiotG0&feature=youtube_gdata'請重新檢查。 – 2012-08-07 07:31:58

+0

我得到TypeError:錯誤#1009:無法訪問空對象引用的屬性或方法。我只是複製你給我的東西然後我自己重新輸入它,它工作:) ty – 2012-08-07 07:38:25

0

試試這個:

(?<==)[^&]*(?=&) 

此正則表達式匹配無論是在「=」和前第一下一個「&」。

+0

這給了一個錯誤的flex:我嘗試了類似的東西之前,事實證明它需要符合一定的條件 – 2012-08-07 07:08:05

相關問題