supe345
supe345

Reputation: 141

regex pattern starting with "=" and exceptions

I'm trying to write an expression which will be used with json files for a vscode extension. My expression should start with "=\s*" and then I want it to select everything after the equal except for the following cases:

  1. TRUE or FALSE after the equality
  2. starting with digit
  3. starting with ' or "

I have tried many things and separately each case I manage to make it work but when I try to put it all together, it doesn't work

Example of doc strings:

abc = test
abc = TRUE
abc = FALSE
abc = "test"
abc = 'test'
abc = 123

Out of these examples my regex should only keep the very first one and "test" can be anything.

What was the closest to the solution was this one /(=\s*)^(((?!TRUE|FALSE|[0-9]|\"|\').)*)$/gm

Upvotes: 1

Views: 455

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627022

You can use

Find what: ^(.*?=)(?!\s*(?:TRUE|FALSE|[0-9"'])).*
Replace With: $1

Details:

  • ^ - start of a line
  • (.*?=) - Group 1: any zero or more chars other than line break chars, as few as possible and then a = char
  • (?!\s*(?:TRUE|FALSE|[0-9"'])) - a negative lookahead that fails the match if, immediately to the right of the current location, there are
    • \s* - zero or more whitespace
    • (?:TRUE|FALSE|[0-9"']) - TRUE, FALSE, digit or " or '
  • .* - the rest of the line.

See the regex demo and the demo screenshot:

enter image description here

Upvotes: 2

Related Questions