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?

 

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=

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.