0

我正在寻找一个正则表达式来解析 a = 和 & 之间的部分以获取第一个 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);

但我仍然遇到错误。

4

2 回答 2

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.
于 2012-08-07T07:19:15.067 回答
0

尝试这个:

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

此正则表达式将匹配 '=' 之后和第一个下一个 '&' 之前的任何内容。

于 2012-08-07T06:56:22.117 回答