Regular Expressions 101

Community Patterns

Community Library Entry

1

Regular Expression
PCRE2 (PHP >=7.3)

/
^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/((\*$)|$|(https?:\/\/)?[\*a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$
/
mg

Description

for JS
regular expression to validate any http|https|ftp|IPv4 address, supported:

  • authority username:password (example https://username:password@example.com);
  • port :0-65565 (example https://example.com:80);
  • query/queries ?c=1 or ?c=1&d=x (example https://example.com/main.php?c=1&d=x);
  • fragment #name (example https://example.com/main.php#top);
  • WaybackMachine URLs [contains *] (example https://web.archive.org/web/*/http://cdn.lijit.com/*);
Submitted by anonymous - 5 months ago