Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

^(https?://)?(www\.)?[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)+([/?].*)?$

Matches optional HTTP/HTTPS URLs with optional 'www.' prefix, followed by a domain name, optional path/query, and ensures the entire string is consumed from start to end.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts the position at the start of the string Start of string
( Start of a non-capturing group (implicit in JS) N/A
https? Literal 'http' followed by an optional 's' http or https
:// Literal colon followed by two forward slashes ://
)? End of group, quantifier: makes the entire group optional (0 or 1 times) Optional 'http://' or 'https://'
( Start of a non-capturing group (implicit in JS) N/A
www\. Literal 'www.' (dot is escaped) www.
)? End of group, quantifier: makes the entire group optional (0 or 1 times) Optional 'www.'
[a-zA-Z0-9-]+ Character class: one or more alphanumeric characters or hyphens example, 123, test-site
( Start of a capturing group N/A
\. Literal dot (escaped) .
[a-zA-Z0-9-]+ Character class: one or more alphanumeric characters or hyphens com, org, site-name
)+ End of group, quantifier: one or more repetitions of the group (greedy) .com, .org, .site-name
( Start of a capturing group N/A
[/?] Character class: either a forward slash or a question mark / or ?
.* Any character (except newline) zero or more times (greedy) /path, ?query=1
)? End of group, quantifier: makes the entire group optional (0 or 1 times) Optional path or query
$ Anchor: asserts the position at the end of the string End of string

Matches

  • http://example.com
  • https://www.example.com/path
  • example.org
  • www.test-site.com?query=1
  • https://sub.domain.co.uk/path/to/page

Does not match

  • ftp://example.com
  • example..com
  • http://example
  • example.com/path?query=1#fragment
  • http://example.com/path with space

Common pitfalls

  • Overly permissive character class ([a-zA-Z0-9-]) allows invalid domain names (e.g., starting/ending with hyphen).
  • Missing validation for top-level domains (e.g., '.com' is valid, but '.c' is not).
  • Greedy quantifiers (+, *) may cause catastrophic backtracking with malformed input.
  • Does not validate internationalized domain names (IDNs).
  • Allows paths with spaces or special characters that may not be URL-encoded.

Performance notes

The pattern is generally efficient for typical URLs, but the greedy quantifiers (+, *) could lead to catastrophic backtracking with malformed input (e.g., long strings of dots or slashes). Anchoring (^ and $) ensures the entire string is validated, preventing partial matches. Consider using possessive quantifiers or atomic groups (if supported) to mitigate backtracking risks.

About regular expressions

Regular expressions are tiny programs that match patterns in text. They're powerful and notoriously cryptic — most engineers can write them but few can read them confidently. Qaynaq Labs' regex explainer breaks any pattern into its constituent tokens, shows what each one does, and runs through both matching and non-matching examples so you can verify the logic.

Different regex flavors (JavaScript, PCRE, Python, Go) have subtle differences in syntax and supported features. Pick your flavor in the input — the explainer accounts for it.

Frequently asked

Why is the domain part ([a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)+) so permissive?

The pattern allows for flexibility in domain names, including subdomains and hyphens, but it does not enforce strict domain validation rules (e.g., no leading/trailing hyphens, valid TLDs). For production use, consider a more rigorous validation library or RFC-compliant regex.

Can this regex match URLs with ports (e.g., http://example.com:8080)?

No. The pattern does not account for ports. To match ports, you would need to add an optional group like (:[0-9]+) after the domain part.

Does this regex support IPv4 or IPv6 addresses?

No. The pattern only matches domain names. To support IP addresses, you would need to add additional alternatives (e.g., (\d{1,3}\.){3}\d{1,3} for IPv4 or IPv6 patterns).

Why is the path/query part ([/?].*)? so broad?

The pattern allows any characters after the domain, including spaces or special characters, which may not be valid in URLs. For stricter validation, consider using a URL parsing library or a more specific regex for paths and queries.