Text Processingflags: gm
JavaScript Variable Declaration
Match JavaScript / TypeScript variable declarations (`var`, `let`, `const`), capturing the keyword and identifier name.
Try it in RegexPro →Available in
Pattern
regexengine-agnostic
\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
\b(var|let|const) captures the declaration keyword at a word boundary. \s+ requires whitespace. ([a-zA-Z_$][\w$]*) captures a valid JS identifier (starts with letter, underscore, or $). \s*(?:=|;|$) matches the assignment or terminator so we don't mis-match function parameters.
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