URL validation regex needs to handle schemes, subdomains, paths, query strings, fragments, and encoded characters while still rejecting plainly malformed strings.
Loading Regex Tester…
Test URL patterns against edge cases: IPs, ports, query strings, fragments
Verify scheme handling for http, https, and custom protocols
Check that subdomains and multi-label TLDs are accepted
Confirm malformed URLs are correctly rejected
Drop the Regex Tester into any page — blog post, product docs, intranet, school portal — with a single line of HTML. Your visitors get the full tool, processed entirely in their browser. No backend, no uploads, no signup.
Embed code
<iframe
src="https://www.fixtools.io/developer/regex-tester?embed=1"
width="100%"
height="780"
frameborder="0"
style="border:0;border-radius:16px;max-width:900px;"
title="Regex Tester by FixTools"
loading="lazy"
allow="clipboard-write"
></iframe>Attribution-friendly: a small "Powered by FixTools" link appears in the embed footer.
A URL has up to eight components: scheme, authority (userinfo, host, port), path, query, and fragment. A regex that validates all components in full detail becomes unwieldy: the RFC 3986 grammar for a generic URI spans over a page of production rules. For web application validation, a practical URL regex focuses on the most common form: an optional scheme (http:// or https://), a hostname consisting of labels separated by dots, an optional port, an optional path, optional query string, and optional fragment. Edge cases that a practical pattern can intentionally omit include IP address literals in bracket notation, IPv6 addresses, percent-encoded characters in the host, userinfo credentials embedded in the URL, and data URIs that embed file content directly. Excluding these keeps the pattern readable and maintainable without meaningfully reducing its usefulness for the vast majority of real-world URLs.
A widely-used practical URL pattern is: ^(https?:\/\/)([\da-z\-.]+)\.([a-z.]{2,6})([\w\-._~:/?#\[\]@!$&'()*+,;=%]*)$. Breaking this down: https?:\/\/ matches http:// or https://; [\da-z\-.]+ matches subdomain and domain labels; the TLD group covers two to six character labels (though extending to {2,} is advisable given long modern TLDs); and the final character class covers all RFC 3986 path, query, and fragment characters. In JavaScript string notation the double backslash is required; when entering directly into a regex tester, single backslashes are the correct form. Always test both the expanded and the compact forms to confirm they match identically.
For scraping and content extraction use cases, the validation question shifts from "is this a valid URL per spec?" to "is this a link worth following?" Extraction patterns are looser and anchor-free, designed to find URL-like substrings within larger blocks of text. Validation patterns are stricter and anchor the entire string with ^ and $. Decide which use case you have before writing the pattern: the two goals produce very different regex designs with different tradeoffs around false positives and false negatives. Mixing the two approaches without awareness of this distinction is the most common source of URL pattern bugs in production code.
URL regex is also a notorious source of catastrophic backtracking. Patterns that allow multiple optional segments combined with broad character classes can explode on malicious inputs designed to maximise the engine's exploration of impossible match paths. The classic dangerous shape is something like ^(https?://)?([a-zA-Z0-9]+\.)+[a-zA-Z0-9]+(/.*)?$ applied to a 200-character input ending with a single disallowed character: the engine explores every possible way to partition the dot-separated host before declaring no match. The fix is to unroll the loop into ^https?://[a-zA-Z0-9]+(\.[a-zA-Z0-9]+)*(/.*)?$ which has no ambiguous overlap between segments. Before deploying any URL regex on a route that accepts user input, run it through a static analyser like safe-regex2 or test it against deliberately pathological strings to confirm worst-case behaviour stays linear.
Paste your URL validation pattern and a list of URLs (one per line) including edge cases: URLs with query strings, fragments, subdomains, ports, and intentionally malformed strings.
Step-by-step guide to regex for url validation, test patterns against real urls:
Open the Regex Tester
Navigate to the FixTools Regex Tester using the link on this page. The browser-based tool requires no setup and activates immediately, so you can begin testing your URL pattern within seconds of arriving on the page.
Enter your URL pattern
Paste your URL validation regex into the pattern field. A practical starting point that handles the most common cases is ^https?:\/\/[\w\-]+(\.[\w\-]+)+[\w\-._~:/?#\[\]@!$&'()*+,;=%]*$. This accepts both HTTP and HTTPS URLs with standard hostnames, paths, query strings, and fragments. Adjust the scheme prefix and character classes to match your specific acceptance criteria.
Enable g and m flags
Use the global and multiline flags to evaluate a full list of URLs in a single test run. Each URL on a separate line is checked independently, giving you a clear visual result for every sample without having to paste and test each URL one by one.
Paste sample URLs
Include a diverse test set: valid URLs with subdomains, paths, query strings, fragments, and port numbers; valid URLs with long TLDs; and deliberately malformed strings such as "not-a-url", "ftp://legacy.example.com", "javascript:alert(1)", and a bare domain without a scheme. Confirming correct rejection of malformed and potentially dangerous inputs is as important as confirming acceptance of valid ones.
Common situations where this approach makes a real difference:
Validating a URL input field in a link-sharing application
A link-sharing app needs to accept https://www.example.com/path?q=1#section while rejecting "javascript:alert(1)", "file:///etc/passwd", and plain text strings entered in the URL field. The pattern ^https?:\/\/[\w\-.]+\.[a-z]{2,}([\w\-._~:/?#\[\]@!$&'()*+,;=%]*)$ accepts the valid URL and rejects all three problematic inputs. The developer tests ftp:// to confirm it is also rejected, and tests a URL with a query string containing an ampersand to confirm the query character class includes it.
Extracting all URLs from a block of scraped HTML text
A web scraper processes raw HTML as plain text and needs to pull all href attribute values from anchor tags. The anchor-free pattern https?:\/\/[\w\-]+(\.[\w\-]+)+[\w\-._~:/?#\[\]@!$&()*+,;=%]* with the g flag finds every HTTP and HTTPS URL in a pasted block of raw HTML. Testing against a sample page's source in the tester confirms the pattern captures full URLs including those with long query strings and hash fragments without stopping prematurely at an unexpected character.
Blocking URLs from a comment field to prevent spam
A comment moderation system needs to flag user submissions containing URLs for human review before publishing. The detection pattern https?:\/\/\S+ is intentionally permissive: it identifies any HTTP or HTTPS URL-like substring in the comment text rather than strictly validating URL structure. Testing against a sample set of known spam comments in the tester confirms all URL-containing submissions are flagged, and testing against comments mentioning "https" in a non-URL context (such as in a sentence) reveals whether the pattern needs a word boundary or space constraint.
Validating webhook URLs in an API integration setup
An API platform validates webhook endpoint URLs before saving them to ensure only reachable HTTPS endpoints are registered. The pattern ^https:\/\/[\w\-]+(\.[\w\-]+)+(:\d{1,5})?(\/.*)? restricts to HTTPS only, supports an optional port number, and accepts any path after the host. Testing confirms https://api.service.com/webhook/v2 and https://internal.company.net:8443/hooks are accepted, while http:// URLs, bare domains without a scheme, and URLs with disallowed characters are all rejected.
Use this page when you need to validate or extract URLs in a form field, web scraper, content moderation system, or data pipeline and want to test your pattern against real URL samples.
Get better results with these expert suggestions:
Use the URL API for validation in modern JavaScript environments
In browser and Node.js contexts, new URL(string) throws a TypeError for malformed URLs and succeeds silently for valid ones, covering all RFC 3986 edge cases including IPv6, IDN hostnames, and percent-encoding without any regex complexity. Use regex for extraction tasks where you need to find URLs inside larger text, and use the URL API for validation tasks where you have a candidate string and need to confirm it is a well-formed URL. Combining both tools gives you fast extraction followed by authoritative validation.
Decide whether to allow non-HTTPS schemes before writing the pattern
For user-submitted link fields, restricting to https:// only by anchoring the scheme as ^https:\/\/ is a security best practice that blocks javascript:, data:, and file: scheme injection attempts. For internal developer tools that process URLs from controlled sources, allowing http:// may be necessary for testing against local servers. Make this decision before writing the pattern and encode it explicitly rather than treating scheme restriction as an afterthought you can add later.
Test URLs with all components: path, query, fragment, and port
Most URL validation bugs appear at the boundaries between components. A pattern that correctly accepts https://example.com may reject https://example.com:8080/path?foo=bar#section because the port or query character class is too restrictive. Build a test set that includes at least one URL exercising each component individually and one URL with all components combined. Run the full set in the tester before marking the pattern ready for use.
Use a character class for path characters rather than .*
Using the RFC 3986 allowed character set [\w\-._~:/?#\[\]@!$&'()*+,;=%]* instead of .* for the path, query, and fragment portion prevents the pattern from greedily consuming content beyond the end of a URL when matching inside a larger block of text. This matters for extraction patterns where .* can span across multiple URLs on the same line, merging two separate links into a single false match.
More use-case guides for the same tool:
Open the full Regex Tester — free, no account needed, works on any device.
Open Regex Tester →Free · No account needed · Works on any device