Reputation: 25880
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:
.
cannot be in the beginning or end.
on its own is not valida..b
) are not allowedWhat 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
a
a.b
a.b.c
_.$.123
Invalid Examples
.
.a
a.
a..b
Upvotes: 0
Views: 143
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