vitaly-t
vitaly-t

Reputation: 25880

RegEx for JavaScript variables with nested names

When I needed to match a simple open-value JavaScript variable, I used the following RegEx:

\s*[a-zA-Z0-9$_]+\s*

Now I need to add support for nested properties, i.e. there can be dots (.) in the name, with the following considerations:

What would be the right/simplest way to amend such a RegEx pattern?

If it makes any difference, I'm only interested in a version that will work under Node.js


Valid Examples

Invalid Examples

Upvotes: 0

Views: 143

Answers (1)

Peter
Peter

Reputation: 1391

okay the simplest way I found is the following regex

^\s*(([a-zA-Z0-9$_][a-zA-Z0-9$_\.]*[a-zA-Z0-9$_])|([a-zA-Z0-9$_]))\s*$

It matches either a string of at least three characters with optional dots in the middle or a single character of the valid set. It works for the data in your example. If you want to match multiple variables in one line you could maybe use a negative look ahead like

\s*(?!\.)[a-zA-Z0-9$_\.]*(?!\.)\s*

The latter expression also works on your test data (http://regexr.com/3gra2) but will catch every whitespace between the variables so you might have to trim the result. I also fear that negative look aheads might become very complex on long texts.

As I mentioned in my comment the only reliable way of extracting variable names from JavaScript code is an abstract syntax treee (AST) parser that also considers keywords like new, var, function etc..

Upvotes: 1

Related Questions