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

TokenDescription
hMatches the literal character 'h'
tMatches the literal character 't'
tMatches the literal character 't'
pMatches the literal character 'p'
sMatches 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

InputExpected
https://example.comMatch
http://sub.domain.org/path?q=1Match
ftp://files.example.comNo Match
https://exampleNo Match
https://my-site.co.uk/page#sectionMatch

Try It — Interactive Tester

//gi
gimsuy

Match Highlighting(3 matches)

https://example.com http://sub.domain.org/path?q=1 ftp://files.example.com https://example https://my-site.co.uk/page#section

Matches & Capture Groups

#1https://example.comindex 0
#2http://sub.domain.org/path?q=1index 20
#3https://my-site.co.uk/page#sectionindex 91
Pattern: 56 charsFlags: giMatches: 3

Ctrl+Shift+C to copy regex

Customize this pattern →