Using regular expressions to find img tags without an alt attribute

前端 未结 8 1774
伪装坚强ぢ
伪装坚强ぢ 2021-01-30 11:37

I am going through a large website (1600+ pages) to make it pass Priority 1 W3C WAI. As a result, things like image tags need to have alt attributes.

What would be the

8条回答
  •  长情又很酷
    2021-01-30 12:17

    Here is what I just tried in my own environment with a massive enterprise code base with some good success (found no false positives but definitely found valid cases):

    ]*\balt=)[^>]*?>
    

    What's going on in this search:

    1. find the opening of the tag
    2. look for the absence of zero or more characters that are not the closing bracket while also …
    3. Checking for the absence of of a word that begins with "alt" ("\b" is there for making sure we don't get a mid-word name match on something like a class value) and is followed by "=", then …
    4. look for zero or more characters that are not the closing bracket
    5. find the closing bracket

    So this will match:

    
    

    But it won't match either of these:

    
    I have a value.
    

提交回复
热议问题