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

TokenDescription
^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 ':'
\sMatches 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

InputExpected
Content-Type: application/jsonMatch
Authorization: Bearer token123Match
Not a headerNo Match
X-Custom-Header: some valueMatch
: missing nameNo Match

Try It — Interactive Tester

//gm
gimsuy

Match Highlighting(3 matches)

Content-Type: application/json Authorization: Bearer token123 Not a header X-Custom-Header: some value : missing name

Matches & Capture Groups

#1Content-Type: application/jsonindex 0
Group 1:Content-Type
Group 2:application/json
#2Authorization: Bearer token123index 31
Group 1:Authorization
Group 2:Bearer token123
#3X-Custom-Header: some valueindex 75
Group 1:X-Custom-Header
Group 2:some value
Pattern: 33 charsFlags: gmMatches: 3

Ctrl+Shift+C to copy regex

Customize this pattern →