DevKits

Validation

Match a URL

Flags: gi

Match http / https URLs including query strings and fragments.

Pattern

Regular expression

https?:\/\/[\w.-]+(?:\.[\w.-]+)+[\w\-._~:/?#\[\]@!$&'()*+,;=]*

How it works

Matches URLs starting with `http://` or `https://`. Domain is at least one label plus a TLD, followed by the optional path/query/fragment characters allowed by RFC 3986.

Matches

  • https://example.com
  • http://api.example.com/v1/users?id=42
  • https://sub.example.co.uk/path#section

Non-matches

  • example.com (no protocol)
  • ftp://files.example.com

Common gotchas

This intentionally rejects protocol-less URLs (example.com) and non-http schemes. If you also need www.example.com without scheme, add an alternative branch. For strict RFC 3986 compliance, prefer a URL parser (URL constructor in JS, urllib in Python).

Language snippets

JavaScript

const re = /https?:\/\/[\w.-]+(?:\.[\w.-]+)+[\w\-._~:/?#\[\]@!$&'()*+,;=]*/gi;
text.match(re);

Python

import re
re.findall(r"https?://[\w.-]+(?:\.[\w.-]+)+[\w\-._~:/?#\[\]@!$&'()*+,;=]*", text)

Go

re := regexp.MustCompile(`https?://[\w.-]+(?:\.[\w.-]+)+[\w\-._~:/?#\[\]@!$&'()*+,;=]*`)
re.FindAllString(text, -1)

Java

Pattern.compile("https?://[\\w.-]+(?:\\.[\\w.-]+)+[\\w\\-._~:/?#\\[\\]@!$&'()*+,;=]*").matcher(text)

Frequently asked questions

What is the regex for URL?

The pattern https?:\/\/[\w.-]+(?:\.[\w.-]+)+[\w\-._~:/?#\[\]@!$&'()*+,;=]* (flags: gi) matches URL. Matches URLs starting with `http://` or `https://`. Domain is at least one label plus a TLD, followed by the optional path/query/fragment characters allowed by RFC 3986.

What does this URL pattern match?

It matches strings like https://example.com, http://api.example.com/v1/users?id=42, https://sub.example.co.uk/path#section, but rejects example.com (no protocol), ftp://files.example.com.

How do I use this pattern in JavaScript, Python, Go, Java?

Ready-to-run snippets are provided for JavaScript, Python, Go, Java. Copy the one for your language from the Language snippets section — each wraps the same core pattern in that language's regex API.

What are common pitfalls with a URL regex?

This intentionally rejects protocol-less URLs (example.com) and non-http schemes. If you also need www.example.com without scheme, add an alternative branch. For strict RFC 3986 compliance, prefer a URL parser (URL constructor in JS, urllib in Python).

Related patterns

Try this pattern in a tool

Try any pattern live in the Regex Tester