Regexp: count amount of quotes before matched string

53 views Asked by At

Let's say I have a string I want to match: banana. The problem is that I want to check whether this string is inside of double quotes. I've read that you can count the amount of double quotes in the whole line:

(?<!')banana(?=[^"]*(?:"[^"]*"[^"]*)*$).*$

However it doesn't match that string if I have input such as this:

"nope" banana "

In this case I still want the string to be matched (avoid nested quotes), so it means I need to count amount of double quotes BEFORE the string. How can I do that? P.S. I use onigurama in vscode extension

1

There are 1 answers

3
Michail On

As I understand a question you need something like this: \G(?>".*?"|.)*?\Kbanana