Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

^(https?://)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b(?:[-a-zA-Z0-9()@:%_\+.~#?&\/=]*)$

Matches URLs with optional http/https and www prefixes, followed by a domain and path/query. Supports common URL characters but may be overly permissive.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts position at start of the string Start of string
(https?://)? Optional group matching 'http://' or 'https://' http:// or https:// or nothing
? Quantifier: 0 or 1 occurrence of the preceding group Absent or present once
(www\.)? Optional group matching 'www.' www. or nothing
? Quantifier: 0 or 1 occurrence of the preceding group Absent or present once
[-a-zA-Z0-9@:%._\+~#=]{1,256} Character class matching 1 to 256 of allowed URL characters (domain part) example.com or sub.domain
\. Literal dot character .
[a-zA-Z0-9()]{1,6} Character class matching 1 to 6 of allowed TLD characters com or org or io
\b Word boundary anchor End of TLD
(?:[-a-zA-Z0-9()@:%_\+.~#?&\/=]*) Non-capturing group matching 0 or more URL path/query characters /path?query=value or / or empty
$ Anchor: asserts position at end of the string End of string

Matches

  • http://example.com
  • https://www.example.com/path?query=value
  • example.com
  • www.example.org
  • https://sub.domain.co.uk/page#section
  • http://user:pass@domain.com
  • https://domain.com/?q=test&lang=en

Does not match

  • ftp://example.com
  • example..com
  • example.c
  • example.comm
  • example.com/path with spaces
  • http://example.com:99999
  • http://example.com/path?query=value#fragment with spaces

Common pitfalls

  • Overly permissive character classes may match invalid or malformed URLs
  • No strict validation of TLD length or structure (e.g., 'com' vs 'c' is allowed)
  • Allows URLs with spaces or other unsafe characters in the path/query
  • Does not enforce valid port numbers (e.g., ':99999' is technically matched)
  • May match strings with invalid domain structures (e.g., 'example..com')
  • No validation of internationalized domain names (IDNs)

Performance notes

Low risk of catastrophic backtracking due to bounded quantifiers ({1,256}, {1,6}, *). Anchors (^ and $) ensure efficient matching. Consider stricter patterns for production use.

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 TLD character class so permissive?

The pattern allows any 1-6 alphanumeric or parenthesis characters for the TLD, which is overly broad. Real TLDs are typically 2-6 characters (e.g., .com, .io) but may include newer longer TLDs (e.g., .photography).

Can this pattern match URLs with authentication (e.g., http://user:pass@example.com)?

Yes, the pattern allows '@' and ':' in the domain part, so it can technically match URLs with authentication. However, this is not recommended for strict URL validation.

Does this pattern validate internationalized domain names (IDNs)?

No, the pattern only allows ASCII characters in the domain part. IDNs (e.g., '例.测试') would not match.

Why is the path/query part so permissive?

The non-capturing group (?:...) allows any combination of allowed characters, including unsafe ones like spaces or unencoded special characters. This is not suitable for strict validation.