Sublime Text's regex search appears to default to multi-line mode, which means the .
won't match line breaks. You can use a mode modifier to use single line mode to make .
match new lines:
(?s)id=("[^"]+").*id=\1
The (?s)
is the single line mode modifier.
However, this regex does a poor job of finding all duplicate keys since it will only match from key
to key
in your sample HTML. You probably need a multi-step process to find all keys, which could be programmed. As others have shown, you'll need to (1) pull all the ids out first, then (2) group them and count them to determine which are dupes.
Alternately, the manual approach would be to change the regex pattern to look-ahead for duplicate ids, then you can find the next match in Sublime Text:
(?s)id=("[^"]+")(?=.*id=\1)
With the above pattern, and your sample HTML, you'll see the following matches highlighted:
<img id="key"> <-- highlighted (dupe found on 3rd line)
<img id="key2"> <-- highlighted (dupe found on 5th line)
<img id="key"> <-- highlighted (next dupe found on last line)
<img id="key3">
<img id="key2">
<img id="key">
Notice that the look-ahead doesn't reveal the actual dupes later in the file. It will stop at the first occurrence and indicates that later on there are dupes.