/^/
EasyProgramming3 min read

Generic Variable Name Regex

Validates a generic variable name shared by most C-family and scripting languages: it must start with a letter or underscore, followed by any number of letters, digits, or underscores.

#variable#identifier#programming#naming-convention#syntax

Regex Pattern

^[a-zA-Z_][a-zA-Z0-9_]*$

Pattern Breakdown

Hover over a token to see what it does.

^[a-zA-Z_][a-zA-Z0-9_]*$
TokenMeaning
^Anchors the match to the start of the string
[a-zA-Z_]The name must begin with an uppercase letter, lowercase letter, or underscore
[a-zA-Z0-9_]*Zero or more letters, digits, or underscores make up the rest of the name
$Anchors the match to the end of the string, ensuring the entire value is a valid variable name

Detailed Explanation

What it does

This pattern checks whether a string is shaped like a generic variable name accepted by most mainstream programming languages (C, C++, JavaScript, Java, Python, Go, etc.): starting with a letter or underscore and continuing with letters, digits, or underscores only, with no spaces, hyphens, or other symbols.

Why it works

Nearly every C-descended language shares the same basic identifier grammar: a non-digit start character followed by any run of word characters. The leading [a-zA-Z_] class enforces the non-digit rule for the first character, the trailing [a-zA-Z0-9_]* class allows digits everywhere else, and the ^/$ anchors make sure the whole string conforms rather than a substring of it.

Common use cases

  • Validating variable names in a cross-language linter, formatter, or style checker
  • Checking form field names or config keys before using them as object property names
  • Filtering or sanitizing user-supplied names in low-code / no-code variable builders
  • Providing live validation feedback in an IDE-style name input field

Edge cases

  • A single underscore, _, is accepted since the repeating group is optional
  • Names with trailing digits, like total2, are valid because digits are allowed anywhere after the first character
  • All-uppercase constants like MAX_VALUE match just as easily as lowercase names
  • A name starting with a digit, like 2fast, is rejected because the first character must come from [a-zA-Z_]

Limitations

  • Does not exclude language-specific reserved keywords (if, class, return, etc.), which are structurally valid but illegal in real code
  • Only covers ASCII letters and ignores Unicode identifier rules that some languages (Python 3, Swift, Kotlin) support
  • Does not enforce a specific naming convention (camelCase, snake_case, etc.) or a maximum length

Interactive Tester

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

count _hidden user1

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 variableNameRegex = /^[a-zA-Z_][a-zA-Z0-9_]*$/;
console.log(variableNameRegex.test('MAX_VALUE')); // true

Common Mistakes

Using \w+ alone (e.g. ^\w+$) and forgetting it allows a name to start with a digit like 123total

Fix: Split the first character into its own [a-zA-Z_] class separate from the repeating [a-zA-Z0-9_]* tail

Assuming this generic pattern also rejects language-specific reserved keywords

Fix: Layer a keyword blocklist check for the specific target language on top of this structural check

Reusing this pattern to enforce a naming convention (camelCase, snake_case), when it only validates general shape

Fix: Combine with a convention-specific pattern like camel-case or snake-case when style matters, not just legality

Performance Notes

  • The two character classes don't overlap in what they consume, so matching runs in linear time with no catastrophic backtracking
  • Anchoring with ^ and $ lets invalid names fail fast on the very first character
  • Lightweight enough to validate on every keystroke in a form or IDE without noticeable lag

Browser Compatibility

EngineSupportedNotes
ChromeYes
FirefoxYes
SafariYes
EdgeYes
Node.jsYes