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
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.