2010-09-17 57 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