Regex for JavaScript Variable Name
Regex Pattern
^[a-zA-Z_$][a-zA-Z0-9_$]*$Valid JavaScript variable/identifier name
Quick Answer
The regex pattern for javascript variable name is `^[a-zA-Z_$][a-zA-Z0-9_$]*$`. Valid JavaScript variable/identifier name. This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.
Test Examples
| Input | Result |
|---|---|
| myVar | ✓ Matches |
| _private | ✓ Matches |
| $jquery | ✓ Matches |
| camelCase123 | ✓ Matches |
| 123abc | ✗ No match |
| -kebab | ✗ No match |
| my var | ✗ No match |
| class | ✗ No match |
Code Examples
javascript
const regex = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/; const isValid = regex.test(value);
python
import re
pattern = r'^[a-zA-Z_$][a-zA-Z0-9_$]*$'
if re.match(pattern, value):
print("valid")ruby
pattern = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/ if value =~ pattern puts "valid" end
php
if (preg_match('/^[a-zA-Z_$][a-zA-Z0-9_$]*$/', $value)) {
echo "valid";
}java
String pattern = "^[a-zA-Z_$][a-zA-Z0-9_$]*$"; boolean isValid = value.matches(pattern);
Frequently Asked Questions
Related Regex Patterns
Python Variable Name
Valid Python identifier name
CSS Class Name
Valid CSS class name (starts with letter, underscore, or hyphen)
CSS ID Selector
CSS ID selector starting with #
JSON Key
JSON object key (quoted string followed by colon)
Semantic Version (Strict)
Strict semantic versioning (MAJOR.MINOR.PATCH)
SemVer with Pre-release
Full semantic version with optional pre-release and build metadata