/^/
MediumWeb5 min read

YAML Key-Value Line Regex

Matches a single YAML line of the form key: value, allowing for leading indentation and optional whitespace around the colon.

#yaml#web#config#parsing#key-value

Regex Pattern

^\s*[A-Za-z_][\w-]*\s*:\s+.+$

Default flags: m

Pattern Breakdown

Hover over a token to see what it does.

^\s*[A-Za-z_][\w-]*\s*:\s+.+$
TokenMeaning
^Anchors to the start of a line (multiline mode via the m flag)
\s*Optional leading indentation before the key
[A-Za-z_][\w-]*The key: starts with a letter or underscore, then letters, digits, underscores, or dashes
\s*:Optional space before the colon separating key and value
\s+At least one space or tab required after the colon, per YAML style
.+The value, which is any run of one or more non-newline characters
$Anchors to the end of the line

Detailed Explanation

What it does

This pattern matches a single line in a YAML document that defines a scalar key-value mapping, such as name: John or age: 30. It tolerates leading indentation for nested mappings and optional spacing around the colon, but requires a non-empty value after it.

Why it works

YAML mappings are indentation-sensitive, so \s* at the start absorbs any nesting level. The key follows typical YAML scalar-key naming (letters, digits, dashes, underscores), and requiring \s+ after the colon distinguishes a real key-value line from a bare key like nested: that introduces a nested block. The m flag lets ^ and $ anchor per line instead of the whole string, which matters when scanning a multi-line document.

Common use cases

  • Extracting flat key-value pairs from a simple YAML config file without pulling in a full YAML parser
  • Validating that a generated YAML snippet's lines follow key: value formatting before writing it to disk
  • Building a lightweight YAML syntax highlighter for a documentation site
  • Linting configuration files for consistent key-value spacing

Edge cases

  • Indented nested keys, like ' database: postgres', are matched because leading whitespace is optional and unbounded
  • A key with no value, like 'nested:' followed by a block on the next lines, is intentionally rejected since \s+.+ requires an inline value
  • A space before the colon, like 'key : value', still matches because of the \s* before the colon
  • List items starting with a dash, like '- item', are correctly rejected since they don't match the key character class

Limitations

  • This pattern only handles simple scalar key-value lines, not YAML block/flow sequences, mappings, anchors, or multi-line block scalars
  • It does not validate that quoted string values have properly balanced quotes
  • Comments after a value on the same line (e.g. 'key: value # note') are swallowed as part of the value rather than stripped

Interactive Tester

Edit the pattern or text below — matching runs live in your browser.

name: John age: 30 indented: value
3 matches

Test Cases

Editable — add your own inputs to see if they pass.

InputExpectedResult
Pass
Pass
Pass
Pass
Pass
Pass
Pass
Pass

Language Variants

Production-ready examples in 12 languages.

const yamlKeyValueRegex = /^\s*[A-Za-z_][\w-]*\s*:\s+.+$/m;
console.log(yamlKeyValueRegex.test('name: John')); // true

Common Mistakes

Requiring exactly one space after the colon with \s instead of \s+, which rejects lines with multiple spaces of alignment padding

Fix: Use \s+ to accept any amount of whitespace after the colon

Forgetting the multiline flag when scanning a full YAML document, so ^ and $ only match the very start/end of the whole string

Fix: Add the m flag (or use per-language multiline options) so each line is anchored independently

Expecting this regex to also capture nested block mappings or list values correctly

Fix: Use a real YAML parser (e.g. js-yaml, PyYAML) for anything beyond flat scalar key-value extraction

Performance Notes

  • The pattern is a straightforward linear scan per line with no nested quantifiers, so it performs well even on large YAML files
  • Using the multiline flag avoids the need to manually split the document into lines before matching
  • For repeated extraction across many files, compile the regex once and reuse it rather than constructing a new RegExp per call

Browser Compatibility

EngineSupportedNotes
ChromeYes
FirefoxYes
SafariYes
EdgeYes
Node.jsYes