Regular Expression To Match Variable Names In Programming Languages

A regular expression to match variable names (also called identifiers) in programming languages.

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

Matches:

  • regexPattern
  • regex_pattern
  • regex

Non-matches:

  • regex pattern
  • regex-pattern
  • regex|pattern