Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

^([A-Za-z]+:)?\/\/[^\/?#]+(\?[^#]*)?(#.*)?$

Matches URLs starting with an optional scheme (e.g., 'http:'), followed by '//', a domain, optional query parameters, and an optional fragment identifier.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts position at start of the string Start of 'http://example.com'
( Start of a non-capturing group (though it captures due to ?: absence) N/A
[A-Za-z]+ Character class: one or more uppercase or lowercase letters http, ftp, HTTPS
: Literal colon character :
)? Quantifier: makes the preceding group (scheme) optional Omitted in '//example.com' or present in 'http://example.com'
\/\/ Literal double forward slash //
[^\/?#]+ Character class: one or more characters that are not '/', '?', or '#' example.com, sub.domain.co.uk
( Start of a capturing group for query parameters N/A
\? Literal question mark (start of query) ?
[^#]* Character class: zero or more characters that are not '#' param=value&key=123
)? Quantifier: makes the preceding group (query) optional Omitted in 'http://example.com' or present in 'http://example.com?foo=bar'
( Start of a capturing group for fragment N/A
# Literal hash (start of fragment) #
.* Quantifier: zero or more of any character (except newline) section1, top
)? Quantifier: makes the preceding group (fragment) optional Omitted in 'http://example.com' or present in 'http://example.com#top'
$ Anchor: asserts position at end of the string End of 'http://example.com/path?query#frag'

Matches

  • http://example.com
  • https://sub.domain.co.uk/path?query=value#section
  • ftp://files.example.org
  • //example.com
  • http://localhost:8080
  • http://example.com?foo=bar
  • http://example.com#top
  • http://example.com/path?query=value#frag

Does not match

  • example.com
  • http:/example.com
  • http//example.com
  • http://example.com/path?query#frag?extra
  • http://example.com#frag?query
  • http://example.com/path?query#frag extra

Common pitfalls

  • Assuming the scheme must be present (it's optional)
  • Not accounting for internationalized domain names (IDNs) which may contain non-ASCII characters
  • Overlooking that the domain part cannot contain '/', '?', or '#'
  • Ignoring that the fragment can contain any characters, including '?'
  • Assuming the query must be present if the fragment is present (both are optional independently)

Performance notes

The pattern is linear in complexity and does not pose a catastrophic backtracking risk. However, overly broad patterns like '.*' in the fragment group can be inefficient for very long strings. Anchors (^ and $) ensure the entire string is matched, which is efficient.

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

Does this pattern validate all valid URLs?

No. It matches a broad subset of URLs but does not validate all possible valid URLs according to RFC standards. For example, it does not handle userinfo (e.g., 'http://user:pass@example.com') or port numbers explicitly (though ports are allowed in the domain part).

Can this pattern match relative URLs?

No. The pattern requires an absolute URL starting with a scheme or '//'.

Why is the scheme group not using a non-capturing group (?:...)?

The group is capturing by default (no '?:'). If capturing is not needed, it should be written as (?:[A-Za-z]+:)? to avoid unnecessary capture overhead.

Does this pattern handle IPv6 addresses in the domain?

No. IPv6 addresses (e.g., 'http://[2001:db8::1]') are not matched by this pattern.