Regex for URLs

This reges can be used to detect an url. It has four groups:
  • Protocol
  • Domain
  • Top Level Domain
  • Subdomains

(https?://)?(\w+\.)+\w{2,4}(/[\w./?:~+\-\\\[\]\*#%&=]*)?

The description of each group:
  • (https?://)? http, https or nothing.
  • * (\w+\.)+ words, numbers, underscore followed by a full stop, repeated one or more times.
  • * \w{2,4} words, numbers, underscore (TLD) of 2 – 4 chars
  • * ((/[\w./?:~+\-\\\[\]\*#%&=]*)? words, numbers, underscore (subdomain, query string).
    • / ? : ~ + – \ [ ] * # % & = ]* is the allowed chars.

Happy regex’ing :)

Tagged on:

Skip to toolbar