2011-08-03 42 views
1

像往常一樣戰鬥與自己瞭解的正則表達式和我需要幫助正則表達式的JavaScript幫助

這裏是字符串:

str = "function onclick(){location.href='http://localhost.com/default.aspx?sectionid=45674356-346-4447-3456-sddwerwertye&languageid=1';}"; 

什麼,我需要的regex參與後:

輸出:

http://localhost.com/default.aspx?sectionid=45674356-346-4447-3456-sddwerwertye&languageid=1 

所以一切之間是什麼:

...{location.href=」 ----和--- 「;}

感謝您的幫助!

回答

1
var str = "function onclick(){location.href='http://localhost.com/default.aspx?sectionid=45674356-346-4447-3456-sddwerwertye&languageid=1';}"; 
var m = str.match("location\.href='([^']*)"); 
var url = m[1]; 
1
/location\.href='([^']+)'/ 

該URL包含在第一組中。

str = "function onclick(){location.href='http://localhost.com/default.aspx?sectionid=45674356-346-4447-3456-sddwerwertye&languageid=1';}"; 
    var pattern = /location\.href='([^']+)'/; 
    if(pattern.test(str)) { 
     return pattern.exec (str)[1]; 
    } 
0

/.*?'([^']*)/怎麼樣?這是「直到第一個撇號忽略,抓住一切不是一個撇號」。

0
str = "function onclick(){location.href='http://localhost.com/default.aspx?sectionid=45674356-346-4447-3456-sddwerwertye&languageid=1';}"; 
str.replace(/.*location.href='(.*)'.*/,"$1"); 

將這工作在您的情況?