Using regular expressions to find img tags without an alt attribute

前端 未结 8 1757
伪装坚强ぢ
伪装坚强ぢ 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:08

    This is really tricky, because regular expressions are mostly about matching something that is there. With look-around trickery, you can do things like 'find A that is not preceded/followed by B', etc. But I think the most pragmatic solution for you wouldn't be that.

    My proposal relies a little bit on your existing code not doing too crazy things, and you might have to fine-tune it, but I think it's a good shot, if you really want to use a RegEx-search for your problem.

    So what I suggest would be to find all img tags, that can (but don't need to) have all valid attributes for an img-element. Whether that is an approach you can work with is for you to decide.

    Proposal:

    //
    

    The current limitations are:

    1. It expects your attribute values to be delimited by double quotes,
    2. It doesn't take into account possible inline on*Event attributes,
    3. It doesn't find img elements with 'illegal' attributes.

提交回复
热议问题