Pattern library / Validation
Regex to Match camelCase (Validate & Split Identifiers)
Validating and splitting camelCase names is one of the most common identifier questions (camelcase regex, regex match camelcase, split camelCase with regex). camelCase is a lowercase first word followed by capitalized segments — so it maps directly onto a repeated-segment pattern: [a-z]+ for the first word, then (?:[A-Z][a-z]*)* for every capital-led segment after it. The same shape handles acronym runs like getHTTPResponse, and a two-pass replace splits any identifier into words without the lookarounds RE2 rejects.
How the pattern works
- Anchored with
^and$— it validates a whole identifier, it does not find one inside text. Drop the anchors and add+on the segment group ([a-z]+(?:[A-Z][a-z]*)+) for search mode: then plain words likeparseno longer match, only names that actually contain a capital. - The trailing
*on the segment group is what lets single lowercase words (get,parse) pass — a lowercase-only name is still a legal identifier. Require at least one capitalized segment by switching it to+. - Digits are excluded on purpose:
utf8Parserandbase64Encodefail. For digit-tolerant identifiers use^[a-z]+(?:[A-Z]?[a-z0-9]*)*$— it is deliberately laxer (a1b2also passes) because digit segments carry no case information to anchor on. - PascalCase class names are rejected by design — accept them with an optional leading capital:
^[A-Z]?[a-z]+(?:[A-Z][a-z]*)*$. No lookahead, lookbehind or backreferences anywhere, so the pattern is RE2-safe and also runs in GA4, BigQuery, Go and Rust — see RE2 vs PCRE.
Test it: match & no-match examples
Every row is loaded straight into the tester — click Try to see the match live:
| Input | Result | Why | |
|---|---|---|---|
userName | Match | classic two-part camelCase: lowercase first word, capitalized second | Try → |
parseJSONString | Match | acronym run — each capital starts a segment (parse · J · S · O · N · String) | Try → |
getHTTPResponse | Match | a capital run before the final word still fits the repeated segment | Try → |
UserName | No match | PascalCase — the leading [a-z]+ demands a lowercase start | Try → |
user_name | No match | snake_case: the underscore is in neither character class | Try → |
utf8Parser | No match | strict form carries no digits — use the digit variant from the notes | Try → |
2ndTry | No match | identifiers cannot start with a digit | Try → |
Where to use it
JavaScript (validate an identifier)
const camel = /^[a-z]+(?:[A-Z][a-z]*)*$/;
camel.test('parseJSONString'); // true
camel.test('UserName'); // false
camel.test('user_name'); // false
JavaScript (split camelCase into words)
// two passes, plain groups only — no lookarounds
const words = s => s
.replace(/([a-z0-9])([A-Z])/g, '$1_$2')
.replace(/([A-Z]+)([A-Z][a-z])/g, '$1_$2')
.toLowerCase();
words('parseJSONString'); // 'parse_json_string'
words('getHTTPResponse'); // 'get_http_response'
HTML input pattern attribute
<input type="text" name="handler"
pattern="[a-z]+(?:[A-Z][a-z]*)*"
title="camelCase identifier, e.g. parseJSONString">
Common mistakes
- A single-segment shape like
[a-z]+[A-Z][a-z]+misses acronym runs: it fails ongetHTTPResponsebecause after[a-z]+it expects exactly one capital followed by lowercase — andHTTPis four capitals. Repeat the segment group instead. - Most "split camelCase" snippets online lean on lookarounds —
[A-Z](?=[A-Z][a-z])|[A-Z]?(?![A-Z])[a-z]+— which RE2 rejects outright, so they fail in GA4, BigQuery and Go. The two-pass replace above does the same job with plain capture groups and works everywhere. - The strict pattern rejects digits, so real names like
utf8Parserfail validation. The digit variant^[a-z]+(?:[A-Z]?[a-z0-9]*)*$accepts them but also passes junk likea1b2— decide how strict your identifier rule needs to be before switching.
FAQ
How do I match camelCase in regex?
Use ^[a-z]+(?:[A-Z][a-z]*)*$ — a lowercase first word followed by any number of capitalized segments. It matches userName, parseJSONString and getHTTPResponse, and rejects UserName, user_name and utf8Parser.
How do I convert camelCase to snake_case with regex?
Two replaces, no lookarounds: first split the lowercase→uppercase boundary with /([a-z0-9])([A-Z])/g → $1_$2, then split acronym→word boundaries with /([A-Z]+)([A-Z][a-z])/g, then lowercase the result. parseJSONString becomes parse_json_string.
What is the difference between camelCase and PascalCase?
camelCase starts with a lowercase letter (userName), PascalCase with an uppercase one (UserName). The pattern above enforces camelCase by leading with [a-z]+; accept both forms with ^[A-Z]?[a-z]+(?:[A-Z][a-z]*)*$.
Related patterns
Snake Case Regex - Validate snake_case Names · Username Regex — Validate 3–20 Char Usernames · JSON Key Regex — Match Property Names — Test & Copy
For GA4-specific patterns, see the GA4 regex cheat sheet, the page path regex guide, the event filter regex guide, the GA4 regex match types, the custom channel grouping guide and RE2 vs PCRE. Browse the full regex pattern library.