Advertisement

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.

The pattern
^[a-z]+(?:[A-Z][a-z]*)*$
Load this pattern into the tester →

How the pattern works

Test it: match & no-match examples

Every row is loaded straight into the tester — click Try to see the match live:

InputResultWhy
userNameMatchclassic two-part camelCase: lowercase first word, capitalized secondTry →
parseJSONStringMatchacronym run — each capital starts a segment (parse · J · S · O · N · String)Try →
getHTTPResponseMatcha capital run before the final word still fits the repeated segmentTry →
UserNameNo matchPascalCase — the leading [a-z]+ demands a lowercase startTry →
user_nameNo matchsnake_case: the underscore is in neither character classTry →
utf8ParserNo matchstrict form carries no digits — use the digit variant from the notesTry →
2ndTryNo matchidentifiers cannot start with a digitTry →

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

Test this pattern on your own data
Paste it into the free tester with your real strings — nothing leaves your browser.
Open the regex tester →

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]*)*$.

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.