Regex to Match HTTP Header Lines
Match HTTP header lines in the format Header-Name: value. Captures the header name and value as separate groups. Free online regex pattern tester.
Regular Expression
/^([A-Za-z][A-Za-z0-9-]*):\s*(.+)$/gm
Token Breakdown
| Token | Description |
|---|---|
| ^ | Anchors at the start of the string (or line in multiline mode) |
| ( | Start of capturing group |
| [A-Za-z] | Character class — matches any one of: A-Za-z |
| [A-Za-z0-9-] | Character class — matches any one of: A-Za-z0-9- |
| * | Matches the preceding element zero or more times (greedy) |
| ) | End of group |
| : | Matches the literal character ':' |
| \s | Matches any whitespace character (space, tab, newline) |
| * | Matches the preceding element zero or more times (greedy) |
| ( | Start of capturing group |
| . | Matches any character except newline (unless dotAll flag is set) |
| + | Matches the preceding element one or more times (greedy) |
| ) | End of group |
| $ | Anchors at the end of the string (or line in multiline mode) |
Detailed Explanation
This regex matches HTTP header lines following the format defined in RFC 7230. Here is the token-by-token breakdown:
^ — Anchors the match at the start of a line. With the m (multiline) flag, this matches at the beginning of each line, not just the beginning of the string.
([A-Za-z][A-Za-z0-9-]*) — Capturing group 1 matches the header field name. It must start with a letter followed by zero or more letters, digits, or hyphens. This covers standard headers like Content-Type, X-Custom-Header, Authorization, and Accept-Encoding.
: — Matches the literal colon that separates the header name from its value.
\s* — Matches optional whitespace after the colon. The HTTP specification allows optional whitespace between the colon and the field value.
(.+) — Capturing group 2 matches the header value. It captures one or more of any character to the end of the line. Header values can contain virtually any printable character.
$ — Anchors the match at the end of a line.
The g flag enables global matching and the m flag enables multiline mode so the anchors work per line. This is essential for parsing multi-line HTTP header blocks.
This pattern is useful for HTTP request and response parsing, debugging network traffic, building proxy servers, and analyzing log files. It correctly separates header names from values for further processing such as building header maps or validating specific headers.
Example Test Strings
| Input | Expected |
|---|---|
| Content-Type: application/json | Match |
| Authorization: Bearer token123 | Match |
| Not a header | No Match |
| X-Custom-Header: some value | Match |
| : missing name | No Match |
Try It — Interactive Tester
Match Highlighting(3 matches)
Matches & Capture Groups
33 charsFlags: gmMatches: 3Ctrl+Shift+C to copy regex
Related Regex Patterns
Regex to Match Content-Type Headers
/^[a-zA-Z]+\/[a-zA-Z0-9.+-]+(?:\s*;\s*[a-zA-Z0-9-]+=(?:"[^"]*"|[^;,\s]+))*$/
Regex to Match Bearer Token Authorization Headers
/Bearer\s+([A-Za-z0-9\-._~+/]+=*)/g
Regex to Match Basic Authentication Headers
/Basic\s+([A-Za-z0-9+/]*={0,2})/g
Regex to Match URLs
/https?:\/\/[\w.-]+(?:\.[a-zA-Z]{2,})(?:\/[\w./?#&=%-]*)*/gi