fizix Posted February 18, 2010 Share Posted February 18, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/192531-need-to-make-one-regex-work-for-this-scenario/ Share on other sites More sharing options...
sader Posted February 18, 2010 Share Posted February 18, 2010 what about this? if (preg_match('/http:\/\/[^";\']+/im', $str, $regs)) { $result = $regs[0]; } Quote Link to comment https://forums.phpfreaks.com/topic/192531-need-to-make-one-regex-work-for-this-scenario/#findComment-1014405 Share on other sites More sharing options...
fizix Posted February 18, 2010 Author Share Posted February 18, 2010 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= Quote Link to comment https://forums.phpfreaks.com/topic/192531-need-to-make-one-regex-work-for-this-scenario/#findComment-1014410 Share on other sites More sharing options...
cags Posted February 18, 2010 Share Posted February 18, 2010 Something like this should work... '#window\.location=["\']?([^";\'\s]+)#i' Quote Link to comment https://forums.phpfreaks.com/topic/192531-need-to-make-one-regex-work-for-this-scenario/#findComment-1014413 Share on other sites More sharing options...
fizix Posted February 18, 2010 Author Share Posted February 18, 2010 Something like this should work... '#window\.location=["\']?([^";\'\s]+)#i' You're a genius! Thank you sir! Quote Link to comment https://forums.phpfreaks.com/topic/192531-need-to-make-one-regex-work-for-this-scenario/#findComment-1014417 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.