A couple of weeks ago, at work, I was helping a colleague make a git hook that did some rudimentary testing of locally-committed code changes before they get accepted into the remote, such that invalid or malformed code would require correction before making it into the central repo.
After a couple frustrating hours, I came up with a regex that matches only those tags that lack alt attributes:
Notes on flexibility:
- + the tag and its attributes may span multiple lines
- + supports attributes whose names include hyphens (i.e. data-* attributes)
+ tags can end with
/>(self-closing) or just
- + attribute values can be in single or double quotes
– all of the tag's attributes must have quoted values
<img src=me.jpg />won't match)
– none of the tag's attribute values may contain quote characters
<img src="<?php 'someString' ?>" />won't match)
Notes on usage:
- this requires a Perl-compatible regular expression (PCRE), which notably doesn't come standard with
git grep, and requires the
-Pflag when used with
- when searching source code, you should use ack instead of grep
- in Python, your regex string literal should be prefixed with
gflag to match all occurrences, instead of only the first (e.g.
If you find this useful, incomplete, or interesting in anyway, drop me a tweet!