# Regex for URL Validation

> A pragmatic URL regex for http and https links, explained token by token, with match examples and variations for protocol-optional matching.

```
/^https?:\/\/[\w.-]+(?:\.[\w-]+)+[^\s]*$/
```

This pattern validates web URLs: an http or https scheme, a dotted hostname, and any path, query, or fragment after it. It's deliberately scoped to the URLs people paste into forms — for full RFC 3986 parsing (ports, userinfo, IPv6 hosts), use your language's URL parser and check the result instead.

## Token by token

| Token | Meaning |
|---|---|
| `^https?` | the scheme: http with an optional s |
| `:\/\/` | the literal :// (slashes escaped) |
| `[\w.-]+` | the first hostname label(s): word characters, dots, hyphens |
| `(?:\.[\w-]+)+` | at least one more dotted label — requires a real domain, not just localhost |
| `[^\s]*` | the rest of the URL: path, query, fragment — anything but whitespace |
| `$` | end of string |

## Examples

- ✓ matches: `https://www.devkult.com`
- ✓ matches: `http://example.com/path?q=1#top`
- ✓ matches: `https://sub.domain.io/a/b`
- ✗ does not match: `ftp://example.com`
- ✗ does not match: `www.example.com`
- ✗ does not match: `https://localhost`

## Variations

- `^(?:https?:\/\/)?[\w.-]+(?:\.[\w-]+)+[^\s]*$` — scheme optional — also accepts www.example.com
- `https?:\/\/[^\s]+` — unanchored — extract URLs out of running text with the g flag

## Language notes

- JavaScript: for validation (not extraction), new URL(s) in a try/catch is often more reliable than any regex.
- The \/ escapes matter only inside /…/ literals; in Python or Go strings, plain / works.

## FAQ

### Why does this reject localhost?

The (?:\.[\w-]+)+ group requires at least one dot in the hostname, which single-label hosts like localhost don't have. For dev tooling, use the scheme-optional variation and drop that group, or special-case localhost.

### Should I use a regex or a URL parser?

For accepting/rejecting user input, a parser (new URL in JS, urllib.parse in Python) plus a scheme check is more correct. Regex wins when you're extracting URLs from a larger body of text, which parsers can't do.

## Related patterns

- [Regex for Email Validation](https://www.devkult.com/regex/email.md)
- [Regex for URL Slug Validation](https://www.devkult.com/regex/slug.md)
- [Regex for IPv4 Address Validation](https://www.devkult.com/regex/ipv4.md)

Open the interactive page: https://www.devkult.com/regex/url