Jump to content

Need to make one regex work for this scenario


fizix

Recommended Posts

Hey all,

 

I've been playing with this for a while and finally decided to ask the experts. I'm trying to match just the URL in the following strings:

 

window.location="http://www.text.com";
window.location='http://www.text.com';
window.location="http://www.text.com"
window.location='http://www.text.com'
window.location=http://www.text.com;
window.location=http://www.text.com

 

This works for all of them except the last one:

 

/window\.location\=["\']?(.*?)["\';]/i

 

Is there any way to make a regex that works for all of them?

 

Link to comment
Share on other sites

what about this?

 

if (preg_match('/http:\/\/[^";\']+/im', $str, $regs)) 
{
$result = $regs[0];
}

 

I'm afraid that wouldn't work for two reasons:

 

1. The address won't necessarily start with http://, it could be a relative address (sorry, should have specified that)

2. I only want to find strings that start with window.location=

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.