Regex javascript help
Like usual fighting with myself to understand regex and a I need a help
here is the string:
str = "function onclick(){location.href='http://localhost.com/default.aspx?sectionid=45674356-346-4447-3456-sddwerwertye&languageid=1';}";
what I need to after regex involved :
Output:
http://localhost.com/def开发者_如何学运维ault.aspx?sectionid=45674356-346-4447-3456-sddwerwertye&languageid=1
so everything what is between :
"...{location.href=
" ---- and --- ";}
"
Thanks for any help !!!
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];
/location\.href='([^']+)'/
The url is contained within the first group.
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];
}
How about /.*?'([^']*)/
?. That's "ignore until the first apostrophe, grab everything that's not an apostrophe".
str = "function onclick(){location.href='http://localhost.com/default.aspx?sectionid=45674356-346-4447-3456-sddwerwertye&languageid=1';}";
str.replace(/.*location.href='(.*)'.*/,"$1");
Would this work in your situation?
精彩评论