aquiseb
aquiseb

Reputation: 989

Match a pattern in line not followed by any character

With regex, I'd like to make sure all variables are defined.

So the regex should match all strings (whatever the number of spaces) that are followed by an equal sign and are not followed by any character on that line. But the equal sign can be followed by spaces, tabs anything other than characters.

Here's the opposite of what I'd like to get: ([^\s]+).*=.*([,\S]+)

// SELECT THESE ONES
const okOne =    
const okTwo=
okThree=
var okFour=

// DON'T GET THESE ONES
const badOne = "badOne"
const badTwo=  "badTwo"
badThree=3
var badFour=$s

Upvotes: 2

Views: 53

Answers (3)

buræquete
buræquete

Reputation: 14678

Very simple, try the following; ^.*=\s*$

Within each line ^$, starts with anything up to an equal sign .*=, should have nothing but empty space or just nothing till the end of line \s*

check online

Upvotes: 2

The fourth bird
The fourth bird

Reputation: 163217

You could match any character non greedy until the first equals sign. Then match 0+ times a space or a tab.

^.*?=[ \t]*$

Explanation

  • ^ Start of the string
  • .*? Match any character non greedy
  • = Match literally
  • [ \t]* Character class which matches 0+ times a space or a tab
  • $ End of the line

Regex demo

Upvotes: 1

Oram
Oram

Reputation: 1673

Try this: ^.*?=\W*$

regexr.com/42fbi

It will match anything before the equal sign (non greedy), then = and then any non-character (\W).

Upvotes: 2

Related Questions