URL Regular Expression – Free, Online, Private
Matches an http or https URL up to the next space or bracket.
- https://tools.tech4dream.com/en/regex
- http://example.com/a
The pattern
The pattern finds a URL that starts with http:// or https:// and continues until whitespace or a character that usually ends a URL in prose: a space, an angle bracket, a single quote, or a double quote. It is a finder for logs and paragraphs. It is not a validator for every URL the WHATWG parser would accept. The sample highlights the two absolute URLs and leaves the bare www host alone.
The source is https?:\/\/[^\s<>'"]+. The flags on this page are i. i means case-insensitive. m means ^ and $ match at line breaks, which is how the sample can show several candidates. g is not set. The tester finds non-overlapping matches, and without g a pattern with no anchor still reports the matches it walks. The box under the title is a tester with this pattern and the sample already filled in. Edit either one. Nothing you type is uploaded. The same tester, without a preset, is the regex tool.
How to read it
http is literal. s? makes the s optional, so both schemes match. :\/\/ is a colon and two slashes. The slashes are escaped because the pattern is also shown inside a JavaScript literal in the snippet, and the escapes are legal in the RegExp constructor. [^\s<>'"]+ is one or more characters that are not whitespace and not the closing punctuation. The i flag makes HTTP match as well as http. There is no check that the host has a dot, so http://localhost/ matches, which is what you want in a log and not always what you want on a public form.
What it will not do
The pattern stops at a space, so a URL that was line-wrapped is cut. It does not check percent-encoding. It will swallow a trailing period in some sentences if the period is not whitespace. Trim punctuation after the match if you are extracting links from prose. ftp, mailto, and protocol-relative // URLs are intentionally absent. Add them as alternatives if you need them, and test the result in the box. Go's RE2 engine accepts this pattern. Lookaheads are not required.
A match is not a parsed value. An email pattern does not send mail, a UUID pattern does not allocate an id, and a password pattern does not store a password. When the next step needs a real parser, use one: the URL constructor, a date library, a semver library, or the converters on this site. The expression is the part you can see.
JavaScript, Python, and Go
The JavaScript snippet uses the RegExp constructor so the source does not have to be escaped for a literal slash. The Python snippet uses a normal string with the same escapes JSON would emit, which keeps \d as a regex digit class. Go's snippet uses a raw string. If the pattern contains a lookahead, the Go snippet says so and does not call MustCompile, because RE2 would reject it at runtime.
JavaScriptconst re = new RegExp("https?:\\/\\/[^\\s<>'\"]+", "i");
const found = text.match(re);
console.log(found ? found[0] : "no match");
Pythonimport re
pattern = re.compile("https?:\\/\\/[^\\s<>'\"]+", re.IGNORECASE)
found = pattern.search(text)
print(found.group(0) if found else "no match")
Gopackage main
import (
"fmt"
"regexp"
)
func main() {
text := ""
re := regexp.MustCompile(`(?i)https?:\/\/[^\s<>'"]+`)
fmt.Println(re.FindString(text))
}
Frequently asked questions
Does this match www.example.com?
No. The scheme is required. That keeps the pattern from eating ordinary dotted words.
Why the i flag?
So HTTP:// and https:// both match. Host names are case-insensitive, and the scheme is too.
Does it validate the host?
No. It only finds a scheme and a run of allowed characters. Parse the match with the URL constructor if you need a host.
Will a trailing comma be included?
Yes, if the comma is not whitespace. Strip punctuation when you extract links from sentences.