JavaScript Variable Declaration
Match JavaScript / TypeScript variable declarations (`var`, `let`, `const`), capturing the keyword and identifier name.
Try it in RegexPro →Available in
JavaScript / ECMAScript code
const re = new RegExp("\\b(var|let|const)\\s+([a-zA-Z_$][\\w$]*)\\s*(?:=|;|$)", "gm");
const input = "const x = 1;\\nlet name = \"alice\";\\nvar count;";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).
Python (re) code
import re
pattern = re.compile(r"\b(var|let|const)\s+([a-zA-Z_$][\w$]*)\s*(?:=|;|$)", re.MULTILINE)
input_text = "const x = 1;\\nlet name = \"alice\";\\nvar count;"
for m in pattern.finditer(input_text):
print(m.group(0))Stdlib `re` module — no third-party dependency. Works on Python 3.6+.
Go (RE2) code
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`(?m)\b(var|let|const)\s+([a-zA-Z_$][\w$]*)\s*(?:=|;|$)`)
input := `const x = 1;\nlet name = "alice";\nvar count;`
for _, match := range re.FindAllString(input, -1) {
fmt.Println(match)
}
}Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.
Pattern
\b(var|let|const)\s+([a-zA-Z_$][\w$]*)\s*(?:=|;|$) (flags: gm)Raw source: \b(var|let|const)\s+([a-zA-Z_$][\w$]*)\s*(?:=|;|$)
How it works
Examples
Input
const x = 1;\nlet name = "alice";\nvar count;Matches
const x =let name =var count;
Input
const { a, b } = objMatches
const { a, b } =
Input
// no declNo match
—Common use cases
- •AST-free quick code analysis
- •Codemods (var → let / const conversions)
- •Linting for var-usage in modern codebases
- •Documentation / refactoring tooling
Related patterns
Unix Environment Variable Reference
Text ProcessingMatch Unix shell environment variable references in both $VAR and ${VAR} forms.
HCL / Terraform Variable Reference
Text ProcessingMatch Terraform / HCL references like `var.name`, `local.foo`, `module.x.output`, or `data.aws_ami.ubuntu.id`.
Java Package Declaration
Text ProcessingMatch Java `package com.example.app;` declarations and capture the dotted package path.
JavaScript Template Literal Placeholder
Text ProcessingMatch `${expression}` placeholders inside JavaScript template literals.