Skip to the content

URL Regex

A regular expression for web addresses that start with http or https, filled in with test lines and explained piece by piece.

Try:

JavaScript syntax, run by your browser's own RegExp engine.

6 matches.

Matches

  1. 1https://example.comindex 0
  2. 2http://example.com/path/to/page?x=1&y=2#topindex 20
  3. 3https://sub.domain.example.org:8443/index 64
  4. 4https://example.com?q=testindex 101
  5. 5https://example.com:99999/index 333
  6. 6https://-example-.com/index 360
Cheat sheet

Characters

.
Any character except a line break
\d \D
A digit 0-9, anything but a digit
\w \W
A letter a-z, a digit or an underscore; anything else
\s \S
White space, anything but white space
[abc] [^abc]
One of a, b, c; any character except those
[a-z0-9]
A character in one of the ranges
\p{L}
A letter in any script (needs the u flag)
\. \\ \/
A literal dot, backslash, slash

How many

a*
Zero or more
a+
One or more
a?
Zero or one
a{3}
Exactly three
a{2,5}
Two to five
a{2,}
Two or more
a*? a+? a??
The same, but as few as possible (lazy)

Positions

^
Start of the text, or of a line with the m flag
$
End of the text, or of a line with the m flag
\b
The edge of a word
\B
Anywhere that is not the edge of a word

Groups

(abc)
A group whose text is remembered
(?<name>abc)
The same, with a name
(?:abc)
A group that is not remembered
a|b
Either a or b
\1 \k<name>
The same text a group matched earlier

Looking around

a(?=b)
a, only when b follows
a(?!b)
a, only when b does not follow
(?<=b)a
a, only when b comes before it
(?<!b)a
a, only when b does not come before it

In the replacement

$1 $2
The text of group 1, group 2
$<name>
The text of a named group
$&
The whole match
$` $'
The text before the match, after the match
$$
A dollar sign
Share this tool:TwitterFacebookLinkedInWhatsAppEmail

The pattern, piece by piece

For web addresses as people type and paste them: http or https, a domain name with an ending, and whatever follows.

/^https?:\/\/(?:[A-Za-z0-9-]+\.)+[A-Za-z]{2,}(?::\d{1,5})?(?:[\/?#]\S*)?$/gm
^The start of the line.
https?:\/\/http:// or https://. Each slash has a backslash in front, so that the pattern can also stand between slashes in JavaScript code.
(?:[A-Za-z0-9-]+\.)+One or more parts of the host name, each followed by a dot: www. or shop.eu.
[A-Za-z]{2,}The ending of the domain: two letters or more.
(?::\d{1,5})?Optional: a colon and a port number of one to five digits.
(?:[\/?#]\S*)?Optional: the rest of the address. It starts with a slash, a question mark or a hash sign and runs on to the first white space.
$The end of the line.

What it gets right

These are the first lines in the box above. With the g and m flags every line is tested on its own, from its first character to its last.

https://example.comMatch
http://example.com/path/to/page?x=1&y=2#topMatch
https://sub.domain.example.org:8443/Match
https://example.com?q=testMatch
example.comNo matchNo http:// or https:// in front.
ftp://example.com/file.txtNo matchAnother scheme.
https://exampleNo matchNo ending behind the host name.
https://exa mple.com/No matchA space in the host name.
mailto:ada@example.comNo matchAn address, but not of a web page.

Where it goes wrong

The last lines in the box are there to show the limits: text the pattern refuses although it is fine, or lets through although it is not.

http://localhost:3000/No matchA host without a dot is normal on your own machine.
http://192.168.1.10/adminNo matchAn IP address is a valid host.
https://bücher.example/No matchA name with an accent is valid. Only its xn-- spelling gets through.
https://ada:secret@example.com/No matchA user name and a password in front of the host are allowed.
https://example.com:99999/MatchPort numbers stop at 65535.
https://-example-.com/MatchA part of a host name cannot start or end with a hyphen.

Before you rely on it

To decide in code whether a string is a usable web address, parsing beats matching. In JavaScript, new URL(text) throws an error on what it cannot read, and after that you check that its protocol is http: or https:. The URL parser shows what such a parse returns.

To find links inside running text, take off the ^ and the $. When a link with a path is followed by a full stop or a closing bracket, that character is swallowed along with it, because \S* takes everything up to the next white space.

The regex tester page explains the flags, has a cheat sheet of the syntax, and tells what happens when a pattern takes too long.

See all 12