Code
JavaScript identifier (ASCII) Regex
Variable, function and property names in JavaScript must start with a letter, underscore or dollar sign, and may continue with those characters plus digits. This pattern checks that rule for ASCII names, which covers almost all real-world code. It is useful in code generators, form builders that turn labels into keys, and tools that decide whether a property can use dot notation or needs brackets. The honest limits are that it does not reject reserved words such as class or return, and it ignores Unicode letters that the language also allows, so treat it as a practical subset.
Pattern (JavaScript)
/^[A-Za-z_$][\w$]*$/Starts at the start of the line/string, then one of the characters A-Za-z_$, then one of the characters \w$, zero or more times, then the end of the line/string.
Limit: ASCII subset only; reserved words are not excluded.
Examples
Matches
- ✓ myVar
- ✓ _private
- ✓ $el
- ✓ camelCase2
Does not match
- ✗ 2fast
- ✗ my-var
- ✗ my var
- ✗ café
Try it
0 matches (JavaScript engine)
myVar
_private
$el
camelCase2
2fast
my-var
my var
caféCode in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^[A-Za-z_$][\w$]*$/g;
const text = "myVar";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^[A-Za-z_$][\w$]*$')
text = 'myVar'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^[A-Za-z_$][\w$]*$/';
$text = 'myVar';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^[A-Za-z_$][\\w$]*$");
String text = "myVar";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^[A-Za-z_$][\w$]*$`)
text := "myVar"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^[A-Za-z_$][\w$]*$");
var text = "myVar";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"^[A-Za-z_$][\w$]*$").unwrap();
let text = "myVar";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^[A-Za-z_$][\w$]*$/
text = 'myVar'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Does this reject reserved words like class?
- No. class has a valid identifier shape, so it matches. Keep a list of reserved words and check against it in code if you generate variable names.
- Why is the dollar sign allowed?
- JavaScript has always permitted $ in identifiers, which is why libraries could use $ as a function name. It is allowed at the start and anywhere after.
- How do I accept Unicode identifiers?
- Use [\p{ID_Start}_$][\p{ID_Continue}$\u200C\u200D]* with the u flag. That follows the language specification, which allows letters from any script.
Related patterns
- C-style block comment/\*[\s\S]*?\*/
- ES module import statement^\s*import\s+(?:[\w$]+(?:\s*,\s*)?)?(?:\{[^}]*\}|\*\s+as\s+[\w$]+)?\s*(?:from\s+)?["'][^"']+["'];?\s*$
- Python function definition^\s*(?:async\s+)?def\s+[A-Za-z_]\w*\s*\(.*\)\s*(?:->\s*[^:]+)?:\s*$
- UUID version 4^[\da-f]{8}-[\da-f]{4}-4[\da-f]{3}-[89ab][\da-f]{3}-[\da-f]{12}$