0

=と&の間の部分を解析して最初の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);

しかし、まだエラーが発生しています。

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 に答える