Regex to Match URLs
Match HTTP and HTTPS URLs in any text with this regex pattern. Captures full URLs with domains, paths, and query strings. Free online regex tester.
Regular Expression
/https?:\/\/[\w.-]+(?:\.[a-zA-Z]{2,})(?:\/[\w./?#&=%-]*)*/gi
Token Breakdown
| Token | Description |
|---|---|
| h | Matches the literal character 'h' |
| t | Matches the literal character 't' |
| t | Matches the literal character 't' |
| p | Matches the literal character 'p' |
| s | Matches the literal character 's' |
| ? | Makes the preceding element optional (zero or one times) |
| : | Matches the literal character ':' |
| \/ | Matches a literal forward slash |
| \/ | Matches a literal forward slash |
| [\w.-] | Character class — matches any one of: \w.- |
| + | Matches the preceding element one or more times (greedy) |
| (?: | Start of non-capturing group |
| \. | Matches a literal dot |
| [a-zA-Z] | Character class — matches any one of: a-zA-Z |
| {2,} | Matches 2 or more times |
| ) | End of group |
| (?: | Start of non-capturing group |
| \/ | Matches a literal forward slash |
| [\w./?#&=%-] | Character class — matches any one of: \w./?#&=%- |
| * | Matches the preceding element zero or more times (greedy) |
| ) | End of group |
| * | Matches the preceding element zero or more times (greedy) |
Detailed Explanation
This regex matches HTTP and HTTPS URLs found in text. Here is the token-by-token breakdown:
https? — Matches the protocol. The literal string 'http' followed by an optional 's' (the ? quantifier makes the preceding character optional), so both http and https are matched.
:// — Matches the literal characters '://' that separate the protocol from the domain. The forward slashes are escaped with backslashes.
[\w.-]+ — Matches the domain name. The \w shorthand matches word characters (letters, digits, underscore), and we also allow dots and hyphens for subdomains and hyphenated domain names. The + requires at least one character.
(?:.[a-zA-Z]{2,}) — A non-capturing group that matches a dot followed by the top-level domain, requiring at least two letters. This ensures the URL has a valid TLD like .com or .org.
(?:/[\w./?#&=%-]) — A non-capturing group repeated zero or more times that matches the path, query string, and fragment portions of the URL. Each segment starts with a forward slash and can contain word characters, dots, slashes, question marks, hash signs, ampersands, equals signs, percent signs, and hyphens.
The g flag finds all matches in the input text, and the i flag makes matching case-insensitive. This pattern handles most common URL formats encountered in plain text.
Example Test Strings
| Input | Expected |
|---|---|
| https://example.com | Match |
| http://sub.domain.org/path?q=1 | Match |
| ftp://files.example.com | No Match |
| https://example | No Match |
| https://my-site.co.uk/page#section | Match |
Try It — Interactive Tester
Match Highlighting(3 matches)
Matches & Capture Groups
56 charsFlags: giMatches: 3Ctrl+Shift+C to copy regex
Related Regex Patterns
Regex for Email Validation
/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/i
Regex to Extract Domain Names
/(?:https?:\/\/)?(?:www\.)?([a-zA-Z0-9-]+(?:\.[a-zA-Z]{2,})+)/gi
Regex to Match URLs with Query Parameters
/https?:\/\/[\w.-]+\.[a-zA-Z]{2,}(?:\/[\w./-]*)*\?[\w=&%.+-]+(?:#[\w-]*)?/gi
Regex to Match Unix File Paths
/^(?:\/[\w.-]+)+\/?$/