JavaScript Variable Declaration in PY
Match JavaScript / TypeScript variable declarations (`var`, `let`, `const`), capturing the keyword and identifier name.
Try it in the PY tester →Pattern
regexPY
\b(var|let|const)\s+([a-zA-Z_$][\w$]*)\s*(?:=|;|$) (flags: gm)Python (re) code
pyPython
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+.
How the pattern 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
—