1

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/default.aspx?sectionid=45674356-346-4447-3456-sddwerwertye&languageid=1

so everything what is between :

"...{location.href=" ---- and --- ";}"

Thanks for any help !!!

4 Answers 4

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];
Sign up to request clarification or add additional context in comments.

Comments

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];
   }

Comments

0

How about /.*?'([^']*)/?. That's "ignore until the first apostrophe, grab everything that's not an apostrophe".

Comments

0
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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.