开发者

Regex javascript help

开发者 https://www.devze.com 2023-03-25 04:17 出处:网络
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

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?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号