Getting alt tags with regex

I am parsing some HTML source. Is there a regex script to find out if alt tags in an html document are empty?

I want to see if alt tags are empty or not.

Is regex a good fit for this or should I be using string manipulation in C #?

0


a source to share


4 answers


You need to parse the HTML and validate the tags, use the following link, it includes a C # library for parsing HTML tags and you can loop through the tags and get the number of tags: Parsing HTML Tags .



+2


a source


If this is valid XHTML, why do you need Regex at all? If you are just looking for the line:

alt=""

... you should find all empty tags alt

.



Anyway, it's not too hard to build a Regex for search given the poorly written HTML markup (especially with spaces):

alt\s*=\s*"\s*"

      

0


a source


If you want to do this, just look at the page, then CSS selectors might be better if your browser supports: not selector.

Install selectorgadget bookmarklet. Activate it on your page and then set the next selector in the input field and hit the enter key.

img:not([alt])

      

If you automate it and have DOM access for HTML, you can use the same selector.

0


a source


Regexes are fundamentally bad at parsing HTML (see Can you give some examples of why it is difficult to parse XML and HTML with regex? For what). You need an HTML parser. See Can you give an example of parsing HTML with your favorite parser? for examples using various parsers.

0


a source







All Articles