Regex to find URL including those that simply start with // (Protocol-relative URLs)

Multi tool use
Multi tool use


Regex to find URL including those that simply start with // (Protocol-relative URLs)



This Regex finds URLs that begin with http and https


http


https


https?://(www.)?[-a-zA-Z0-9@:%._+~#=]{2,256}.[a-z]{2,6}b([-a-zA-Z0-9@:%_+.~#?&//=]*)



I am trying to figure out how to modify this to including those URLs which omit the http or https part. I understand that these are called 'Protocol-relative URLs'


http


https



example: //example.com and not http://example.com


//example.com


http://example.com




1 Answer
1



Simply make the protocol part optional:


(https?:)?(//)?(www.)?[-a-zA-Z0-9@:%._+~#=]{2,256}.[a-z]{2,6}b([-a-zA-Z0-9@:%_+.~#?&//=]*)



By the way I assume you really wanted example.com and not //example.com (since no one writes a URL that way).


example.com


//example.com





paulirish.com/2010/the-protocol-relative-url it looks like it no longer in use for security reasons, but for my purposes I want to be able to capture urls that are still in this format
– WillD
Jul 3 at 3:31





I see. I've made both the protocol and // optional then.
– blhsing
Jul 3 at 3:34


//






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

ZbmJC9
BiEGDBYEQF0xC k2DlUNT2C5Ek1cGV8N3CJpLAeTEKeXM,7KpoRPyx h1MQHW,rkavXfRhzJZn SBIjf 2Mlnyv

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications