开发者

Find Title Attribute & Link Title in HTML using REGEX

I used <a[^>]*?title=\"([^\"]*?\"[^>]*?> 开发者_如何学Pythonand found all Links with title tags. How can I find all Links with no title tags & title attribute? And how can I find all image ALT tags that are empty or have no ALT tag?


See this classic post on SO RegEx match open tags except XHTML self-contained tags


@Fredrik has you covered pretty well, I think, but here's an alternate general method for this kind of sophisticated find/replace in markup.

Since I'm no regex guru, I like to use jQuery + browser debugger tools + copy/pasting for this kind of thing. I view the page in Firefox(Chrome/dev tools works great, too), open up the Firebug console, and perform the actions in jQuery goodies, something like this:

$('a').each(function(){
  if ($(this).filter('[title]').length == 0) {
    //if there's no title attr
  } else if ($(this).attr('title') == "") {
    //if title is empty empty
  } 
  // etc.
});

// repeat pattern for imgs...

When you're done with your manipulations, copy the relevant section from the debugger (or just grab the whole <body>) and paste it back into your editor.

I find this method much easier to understand than regexes, but that's just because I'm not too bright. HTH.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜