2010-09-17 45 views
1

我有以下文字我如何獲得價值與JavaScript正則表達式

http://www.mydomain.com/#!/account/?id=17 

和我得到#與window.location.href這是給我

#!/account/?id=17 

現在我想提取該值與preg_match或任何正則表達式,因爲該ID值是動態的... ... 實際上,我正在加載數據與jquery ajax ...請讓我知道得到的值的確切表達式...

回答

2

嘗試此:

"#!/account/?id=17".match(/id=(.*)/)[1] 

EDIT

var result = window.location.href.match(/id=(.*)/); 
  • 匹配返回具有兩個位置的數組:
    • 結果[0] =所有匹配的字符串(」 id = 17「)
    • 結果[1] =第一組匹配( 「17」)
+0

但如何可以匹配帳戶/?ID = 17作爲一個整體 – 2010-09-17 17:10:47

+0

「#!/帳戶/?ID = 17」 .match (/#\ !(.*)/)[1] – 2010-09-17 17:15:14

+0

「#!/ account /?id = 17」.match(/#\!\ /(。*)/)[1] – Topera 2010-09-17 17:35:38