2011-02-04 66 views
1

我需要一些簡單的幫助正則表達式我想抓住括號和等號之間的值。jQuery的正則表達式的幫助

 
<a href="[url=img.php?i=1][pod=2]">My Link</a> 

然後抓住URL = img.php?I = 1莢的值 =

所以不知何故正則表達式應該檢查[之間=然後得到=之間的值

+1

爲什麼你有這樣的鏈接?這不是有效的網址。 – 2011-02-04 03:50:24

回答

2

嘗試這個樣品爲自己 - http://jsfiddle.net/ENwf8/

var string = "[url=img.php?i=1][pod=2]"; 
var regEx = /\[(.*?)=(.*?)]\[(.*?)=(.*?)]/; 
var matches = string .match(regEx); 

for (index = 0; index < matches.length; index++) { 
    document.write(index + ": " + matches[index] + "<br>"); 
} 

打印:

0: [url=img.php?i=1][pod=2] 
1: url 
2: img.php?i=1 
3: pod 
4: 2 
3
var matches = "<a href=\"[url=img.php?i=1][pod=2]\">My Link</a>".match(/\[url=(.*)]\[pod=(.*)\]">/); 

var url = matches[1]; // == img.php?i=1 
var pod = matches[2]; // == 2 

你走了!