Regular Expressions 101

Community Patterns

Match ONLY valid URLs!

1

Regular Expression
PCRE2 (PHP >=7.3)

/
(?n)^(https?:\/\/)?((?!-)[a-z\d-]{2,63}(?<!-)\.)+[a-z]{2,24}(\.[a-z]{2})?(\/(([a-z\.]+\/?)+|[\w-]*\?(&?\w+=[^&#]+)+|#\w+(\/\w+)?)+)?$
/
gi

Description

Match all valid URLs

Examples:

  • Valid URLs:
  1. https://www.example.com/
  2. http://subdomain.example.net/
  3. https://www.subdomain.example.com.uk/params
  • Invalid URLs:
  1. htp://invalid.com/
  2. https//invalid.net/
  3. https://invalid.org./
  4. http://.invalid_url.net/

See all 62 URL samples in "Unit Tests"!

Note: All URL samples are made by ChatGPT!

Submitted by Henriii102 - 7 months ago (Last modified 4 months ago)