I would like to know if it's possible to search in my code for a term ("<img") which in the same line of code it does not contain another term ("alt=").
In other words, I would like to know if it's possible to search for all image tags that does not have the alt attribute in it and how to do it.
For example, I need to find these:
<img src="some-image.png" />
But in this search I need to ignore those which already have the attribute:
<img src="another-image.png" alt="Some text" />
Is this possible on Visual Studio Code? If not, is it possible on the Visual Studio (IDE)?
CodePudding user response:
I saw there is a right answer but it took me a long time to figure the right regex for this :) so I thought it might help someone.
you have to press CTRL F5
, then choose the use regular expression option, then paste
^(?!.*\b(alt)\b)(?=.*\b(img)\b).*$
the first group rejects the word alt wherever it appears and the second group searches for the word img.
hope this could help!
CodePudding user response:
In the VS Code search box (ctrl f), or in the search sidebar, there are 3 icons in the search text box. The first is Match Case, second is Match Whole Word, and the third is Use Regular Expression. That is the one you want to select. After selecting that, paste this regex into the search text box.
^(?!.*?alt=).*img\ssrc.*
You can search the document using Ctrl f or search everywhere with the sidebar search tool and it will only match image tags without the alt attribute.
CodePudding user response:
Here is a regex for a couple more test cases: (1) img
tag has an alt
attribute before the src
attribute - shouldn't be found I assume and (2) two or more img
tags on the same line. These cases may or not be of concern.
Find: (<img\s)(?![^>]*alt=)([^>]*>)