0

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
  • a single . on its own is not valid
  • repeated dots (a..b) are not allowed

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

  • a
  • a.b
  • a.b.c
  • _.$.123

Invalid Examples

  • ``
  • .
  • .a
  • a.
  • a..b
11
  • should bracket notation also be valid? i mean you could write a["b"] for example which is the same as a.b. Also variablenames cannot begin with a digit so I would look for a more perfect solution. Commented Sep 27, 2017 at 8:27
  • @Manticore Good question! In my case - no, I do not need brackets ;) Commented Sep 27, 2017 at 8:28
  • 1
    here is my approach: (?!\n)\s*[\w$]+(\.\w+)*(?=\s*\=.*?\;) Commented Sep 27, 2017 at 8:30
  • Could you please share a testing snippet? See ^[a-zA-Z_$][\w$]*(?:\.[\w$]+)*$, too. Commented Sep 27, 2017 at 8:33
  • here: regex101.com/r/uxRssP/2 but I guess I misunderstood your question, looking at your own regex fiddle Commented Sep 27, 2017 at 8:34

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

A little trouble with the .. rule - should not allow repeated dots in the name ;)
I accepted the answer, even though the last requirement for .. wasn't met. But I went for a much simpler and faster approach - to accept all dots, and check for validity later. So, basically I didn't need the answer in the end. But thank you anyway!
oh right, forgot about the double dots, sorry. I was wondering about why it has to be all-regex, so I'm glad you found a faster approach. Thanks for accepting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.