Capturing Variable Names Alone

Now let's write the regular expression for the problem discussed in the previous lesson and verify it using JS code.

Looking at our code, we can easily find the pattern. Variable names have one of the following characters next to them: a blank space, a dot, a closing parenthesis, or a semicolon. This is true for our example code, but we could make this a bit more generic by extending that definition if we had to.

Regular expression

By adding that condition, we can use a conditional match, also known as a positive lookahead (if you don’t remember what that is, go back to chapter 2, and once you’re done, get back here). But to make the lookahead work, we need to stop matching only the lowercase and uppercase letters and start capturing the different sections of the word so we can be sure to ignore words that aren’t followed by one of our characters.

Notice how this example is assuming our camel case variables are only formed by two words. We’ll leave it as an exercise for you to expand the following expression to work for longer words.

And here is the final result:

(\[a-z]+)([A-Z\][a-z]*)(?=[ |\)|\.|;])

The first group captures the lowercase half of the word, while the second group captures the second half as long as it’s followed by one of our predefined characters.

Verification with JS code

And, with it, if we use the same text editor with the JavaScript code from above:

Get hands-on with 1400+ tech skills courses.