# Regex for a URL

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

**Match a whole string consisting of "h", then "t", then "t", then "p", then optionally "s", then ":", then "/", then "/", then one or more of a word character (letter, digit, or underscore) or "-", then one or more of (".", then one or more of a word character (letter, digit, or underscore) or "-"), then optionally ("/", then zero or more of a word character (letter, digit, or underscore), "-", ".", "_", "~", ":", "/", "?", "#", "[", "]", "@", "!", "$", "&", "'", "(", ")", "*", "+", ",", ";", or "=").**

## Step by step

- `^` — Anchor to the start of the input.
- `h` — Match "h".
- `t` — Match "t".
- `t` — Match "t".
- `p` — Match "p".
- `s?` — Match optionally "s".
- `:` — Match ":".
- `/` — Match "/".
- `/` — Match "/".
- `[\w\-]+` — Match one or more of a word character (letter, digit, or underscore) or "-".
- `(\.[\w\-]+)+` — Match one or more of (".", then one or more of a word character (letter, digit, or underscore) or "-").
- `(\/[\w\-._~:/?#[\]@!$&'()*+,;=]*)?` — Match optionally ("/", then zero or more of a word character (letter, digit, or underscore), "-", ".", "_", "~", ":", "/", "?", "#", "[", "]", "@", "!", "$", "&", "'", "(", ")", "*", "+", ",", ";", or "=").
- `$` — Anchor to the end of the input.

## Warnings

### Anchors match string ends, not line ends (info)

Without the m flag, ^ and $ match only the very start and end of the input — not the start and end of each line. On multi-line input this is often not what was intended.

**Fix:** Add the m flag if the pattern should apply per line.

## Details

- Capture groups: 2

---

Canonical URL: https://regex-explainer.gumballtools.com/regex/regex-for-url
JSON API: `GET https://regex-explainer.gumballtools.com/api/v1/explain?pattern=...`
MCP endpoint: `https://regex-explainer.gumballtools.com/api/mcp`
