BytePane

JavaScript Variable Regex Pattern

Matches JavaScript variable declarations (const, let, var) and captures the variable name.

</>
Code & Programming
Intermediate
Difficulty
JavaScript
Language
g
Flags
// Regular Expression
/\b(const|let|var)\s+([a-zA-Z_$][a-zA-Z0-9_$]*)\b/g

Live Regex Tester

Pattern Breakdown

\b(const|let|var)\s+([a-zA-Z_$][a-zA-Z0-9_$]*)\b
Character class [ ]
Group ( )
Quantifier { }
Anchor ^ $
Repetition * + ?
Escape \
Alternation |
Any char .

Code Examples

JavaScript

const regex = /\b(const|let|var)\s+([a-zA-Z_$][a-zA-Z0-9_$]*)\b/g;
const test = "const myVar = 5";
console.log(regex.test(test)); // true

// Extract matches
const matches = test.match(regex);
console.log(matches);

Python

import re

pattern = r'\b(const|let|var)\s+([a-zA-Z_$][a-zA-Z0-9_$]*)\b'
test = "const myVar = 5"
match = re.findall(pattern, test)
print(match)  # Found!

Go

package main

import (
    "fmt"
    "regexp"
)

func main() {
    re := regexp.MustCompile(`\b(const|let|var)\s+([a-zA-Z_$][a-zA-Z0-9_$]*)\b`)
    fmt.Println(re.MatchString("const myVar = 5")) // true
}

Common Use Cases

Code analysislintingrefactoring tools

Match Examples

InputResult
const myVar = 5Match
function fooNo Match

About the JavaScript Variable Regex

Matches JavaScript variable declarations (const, let, var) and captures the variable name.

Regular expressions (regex) are powerful pattern matching tools used across virtually all programming languages. The javascript variable pattern is classified as intermediate difficulty in the code & programming category. This pattern is specifically designed for JavaScript.

When using this regex, always consider edge cases and test thoroughly with real-world data. Use the interactive tester above to validate the pattern against your specific inputs before deploying to production.

Need More Regex Patterns?

Browse our complete library of 100+ regex patterns with interactive testers.

Frequently Asked Questions

What is the JavaScript Variable regex pattern?

Matches JavaScript variable declarations (const, let, var) and captures the variable name.

How do I use the JavaScript Variable regex?

Use the pattern /\b(const|let|var)\s+([a-zA-Z_$][a-zA-Z0-9_$]*)\b/g in your code. In JavaScript: new RegExp('\b(const|let|var)\s+([a-zA-Z_$][a-zA-Z0-9_$]*)\b', 'g'). Test it above with your own input.

What does this JavaScript Variable regex match?

This pattern matches: "const myVar = 5". It does NOT match: "function foo". Code analysis, linting, refactoring tools.

Is the JavaScript Variable regex beginner-friendly?

This pattern is rated Intermediate. It uses some advanced features like character classes and quantifiers.

What languages support the JavaScript Variable regex?

This pattern works in JavaScript. Syntax may vary slightly between regex engines.

Can I modify the JavaScript Variable regex for my use case?

Yes! Use the interactive tester above to modify the pattern and test with your own data. Common modifications include making it case-insensitive (add 'i' flag), matching globally (add 'g' flag), or adjusting character classes.

Related Tools