Home > Back-end >  Negative lookahead in vscode
Negative lookahead in vscode

Time:06-01

I want to find all <img /> tags in my markup, which do not contain the bootstrap img-fluid class.

So this is good: <img blah="qux" />.
And this is bad: <img blah="qux" /> because it's missing img-fluid.

I tried a negative lookahead: <img.*(?!fluid) which doesn't work.

How do I do this?

CodePudding user response:

<img(?:(?!fluid).)*\/>

Here is an example.

A pattern that is handy in cases like these is to replace .* with (?:(?!<arbitrary excluded pattern>).)*.

(?: starts a non capturing group. the ?: portion could be omitted and it would become a capturing group, which you may not need.

Then the negative lookahead is used, followed by the . for any character. The non-capturing group has the * following it, meaning 0 or more instances of the entire group. The whole thing behaves like .*, except that it won't match if the <arbitrary excluded pattern> shows up along the way.

  • Related