Regular Expressions 101

Community Patterns

1

URL-http(s)

Created·2016-05-19 04:49
Flavor·PCRE (Legacy)
Tests for valid http or https protocol URL. *2 groups- one for protocol,domain, and any subdomains; and one for path, query, params, and fragments(if any. The following WILL match: http://foo.com/blah_blah http://foo.com/blah_blah/ http://foo.com/blah_blah_(wikipedia) http://foo.com/blah_blah_(wikipedia)_(again) http://www.example.com/wpstyle/?p=364 https://www.example.com/foo/?bar=baz&inga=42&quux The following WILL NOT match: http://✪df.ws/123 http://userid:password@example.com:8080 http://userid:password@example.com:8080/ http://userid@example.com http://userid@example.com/ http://userid@example.com:8080 http://مثال.إختبار http://例子.测试 http://उदाहरण.परीक्षा http://-.~_!$&'()*+,;=:%40:80%2f::::::@example.com http://1337.net http://a.b-c.de http://223.255.255.254 The following SHOULD NOT and DO NOT match: http:// http://. http://.. http://../ http://? http://?? http://??/ http://# http://## http://##/ http://foo.bar?q=Spaces should be encoded // //a ///a /// http:///a foo.com rdar://1234 h://test http:// shouldfail.com :// should fail http://foo.bar/foo(bar)baz quux ftps://foo.bar/ http://-error-.invalid/ http://a.b--c.de/ http://-a.b.co http://a.b-.co http://0.0.0.0 http://10.1.1.0 http://10.1.1.255 http://224.1.1.1 http://1.1.1.1.1 http://123.123.123 http://3628126748 http://.www.foo.bar/ http://www.foo.bar./ http://.www.foo.bar./ http://10.1.1.1 I only needed http and https; however others could be added.
Submitted by Brennan
1

URL (URI) - TOTAL Breakdown: Named Groups (Supporting Literal, Relative, Dynamic URLs, with/without Filename/Ext, Query Parameters and/or Anchor Tags)

Created·2023-09-08 16:51
Updated·2023-09-08 16:54
Flavor·JavaScript
Need to FULLY Break Out EVERY Component of a URL? Like, as in "every constituent element individually accessible in its own named capture group?" Need it to be pre-optimized and fully ECMA-compliant? Want to see exhaustive unit tests proving these claims (144 are included; all 144 execute in 6.5ms. Total.)? Then you just lucked out. What Does It Do? Fully breaks down a URL, acting the same (meaning the same capture group names apply regardless of use case; makes for MUCH easier interaction in ones code) on: Relative Paths (./, .//) Absolute Paths (/) Fully-qualified domains (https://www.domain.com/) Partially-qualified domains (https://domain.com/) Dynamically-protocol'd domains (//domain.com/) ...with each separated out into HOST, PROTOCOL, DOMAIN, and RELATIVE path prefix. BUT WAIT! THERE'S MORE! It then further proceeds to break out (also into individually-named groups, but only when present): URI Path (/some/path/to/a/file/) Full File Name, with or without Extension (filename.ext) File Name (filename) Extension (.ext) QueryString Parameters (?query=string) Anchor Tags/Routing (#SomeAnchor) Best of all, EVERY ONE of these are delivered in named capture groups (so you're looking at obj.groups.DOMAIN instead of object[3]) Examples: Fully-Qualified Domain Full Request: https://www.domain.com/some/path/to/a/file-with.ext?Plus=QueryString#AndAnchor Host: https://www.domain.com/ Protocol: https:// Domain: www.domain.com/ Path: some/path/to/a/ FullFileName: file-with.ext FileName: file-with Extension: .ext QueryString: ?Plus=QueryString Anchor: #AndAnchor Relative Path Full Request: ../../../../some/path/to/a/file-with.ext?Plus=QueryString#AndAnchor Relative: ../../../../ Path: some/path/to/a/ FullFileName: file-with.ext FileName: file-with Extension: .ext QueryString: ?Plus=QueryString Anchor: #AndAnchor
Submitted by @JJ for Nerdy Deeds, LLC
1

Youtube URL Regex

Created·2016-09-28 10:29
Flavor·PCRE (Legacy)
Youtube URL validator Credit to: phuc77 The captured groups are: protocol subdomain domain path video code query string Works with the following URLs: https://www.youtube.com/watch?v=DFYRQ_zQ-gk&feature=featured https://www.youtube.com/watch?v=DFYRQ_zQ-gk http://www.youtube.com/watch?v=DFYRQ_zQ-gk //www.youtube.com/watch?v=DFYRQ_zQ-gk www.youtube.com/watch?v=DFYRQ_zQ-gk https://youtube.com/watch?v=DFYRQ_zQ-gk http://youtube.com/watch?v=DFYRQ_zQ-gk //youtube.com/watch?v=DFYRQ_zQ-gk youtube.com/watch?v=DFYRQ_zQ-gk https://m.youtube.com/watch?v=DFYRQ_zQ-gk http://m.youtube.com/watch?v=DFYRQ_zQ-gk //m.youtube.com/watch?v=DFYRQ_zQ-gk m.youtube.com/watch?v=DFYRQ_zQ-gk https://www.youtube.com/v/DFYRQ_zQ-gk?fs=1&hl=en_US http://www.youtube.com/v/DFYRQ_zQ-gk?fs=1&hl=en_US //www.youtube.com/v/DFYRQ_zQ-gk?fs=1&hl=en_US www.youtube.com/v/DFYRQ_zQ-gk?fs=1&hl=en_US youtube.com/v/DFYRQ_zQ-gk?fs=1&hl=en_US https://www.youtube.com/embed/DFYRQ_zQ-gk?autoplay=1 https://www.youtube.com/embed/DFYRQ_zQ-gk http://www.youtube.com/embed/DFYRQ_zQ-gk //www.youtube.com/embed/DFYRQ_zQ-gk www.youtube.com/embed/DFYRQ_zQ-gk https://youtube.com/embed/DFYRQ_zQ-gk http://youtube.com/embed/DFYRQ_zQ-gk //youtube.com/embed/DFYRQ_zQ-gk youtube.com/embed/DFYRQ_zQ-gk https://youtu.be/DFYRQ_zQ-gk?t=120 https://youtu.be/DFYRQ_zQ-gk http://youtu.be/DFYRQ_zQ-gk //youtu.be/DFYRQ_zQ-gk youtu.be/DFYRQ_zQ-gk https://www.youtube.com/HamdiKickProduction?v=DFYRQ_zQ-gk
Submitted by Haroun SMIDA

Community Library Entry

1

Regular Expression
Created·2016-05-19 04:49
Flavor·PCRE (Legacy)

/
(https?:\/\/(?:[[:alnum:]]+\.)?[[:alnum:]]+\.com)(\/\S*)
/
g
Open regex in editor

Description

Tests for valid http or https protocol URL. *2 groups- one for protocol,domain, and any subdomains; and one for path, query, params, and fragments(if any.

The following WILL match: http://foo.com/blah_blah http://foo.com/blah_blah/ http://foo.com/blah_blah_(wikipedia) http://foo.com/blah_blah_(wikipedia)_(again) http://www.example.com/wpstyle/?p=364 https://www.example.com/foo/?bar=baz&inga=42&quux

The following WILL NOT match: http://✪df.ws/123 http://userid:password@example.com:8080 http://userid:password@example.com:8080/ http://userid@example.com http://userid@example.com/ http://userid@example.com:8080 http://مثال.إختبار http://例子.测试 http://उदाहरण.परीक्षा http://-.~_!$&'()*+,;=:%40:80%2f::::::@example.com http://1337.net http://a.b-c.de http://223.255.255.254

The following SHOULD NOT and DO NOT match: http:// http://. http://.. http://../ http://? http://?? http://??/ http://# http://## http://##/ http://foo.bar?q=Spaces should be encoded // //a ///a /// http:///a foo.com rdar://1234 h://test http:// shouldfail.com :// should fail http://foo.bar/foo(bar)baz quux ftps://foo.bar/ http://-error-.invalid/ http://a.b--c.de/ http://-a.b.co http://a.b-.co http://0.0.0.0 http://10.1.1.0 http://10.1.1.255 http://224.1.1.1 http://1.1.1.1.1 http://123.123.123 http://3628126748 http://.www.foo.bar/ http://www.foo.bar./ http://.www.foo.bar./ http://10.1.1.1

I only needed http and https; however others could be added.

Submitted by Brennan