slang's solution to "Find missing or empty alt tags in images via regex"

Please don't use a regex, use JSDOM<\/a>, an HTML parser, or whatever... just don't try to process a structured language with as loose syntax as HTML via regex. Off the top of my head, it would look something like this:<\/p>\n

var<\/span> <\/span>jsdom<\/span>,<\/span> <\/span>nodefn<\/span>,<\/span> <\/span>request<\/span>;<\/span>\nrequest<\/span> <\/span>=<\/span> <\/span>require<\/span>(<\/span>'request-promise');<\/span>\njsdom<\/span> <\/span>=<\/span> <\/span>require<\/span>(<\/span>'jsdom');<\/span>\nnodefn<\/span> <\/span>=<\/span> <\/span>require<\/span>(<\/span>'when/node');<\/span>\n\nrequest<\/span>.<\/span>get<\/span>({<\/span>\n  <\/span>uri:<\/span> <\/span>'https://twitter.com'<\/span>\n})<\/span>.<\/span>then<\/span>(<\/span>function<\/span>(<\/span>response<\/span>)<\/span> <\/span>{<\/span>\n  <\/span>return<\/span> <\/span>nodefn<\/span>.<\/span>call<\/span>(<\/span>jsdom<\/span>.<\/span>env<\/span>,<\/span> <\/span>response<\/span>);<\/span>\n})<\/span>.<\/span>then<\/span>(<\/span>function<\/span>(<\/span>window<\/span>)<\/span> <\/span>{<\/span>\n  <\/span>var<\/span> <\/span>altAttr<\/span>,<\/span> <\/span>img<\/span>,<\/span> <\/span>_i<\/span>,<\/span> <\/span>_len<\/span>,<\/span> <\/span>_ref<\/span>;<\/span>\n  <\/span>_ref<\/span> <\/span>=<\/span> <\/span>window<\/span>.<\/span>document<\/span>.<\/span>querySelector<\/span>(<\/span>'img');<\/span>\n  <\/span>for<\/span> <\/span>(<\/span>_i<\/span> <\/span>=<\/span> <\/span>0<\/span>,<\/span> <\/span>_len<\/span> <\/span>=<\/span> <\/span>_ref<\/span>.<\/span>length<\/span>;<\/span> <\/span>_i<\/span> <\/span><<\/span> <\/span>_len<\/span>;<\/span> <\/span>_i<\/span>++<\/span>)<\/span> <\/span>{<\/span>\n    <\/span>img<\/span> <\/span>=<\/span> <\/span>_ref<\/span>[<\/span>_i<\/span>];<\/span>\n    <\/span>altAttr<\/span> <\/span>=<\/span> <\/span>img<\/span>.<\/span>getAttribute<\/span>(<\/span>'alt');<\/span>\n    <\/span>if<\/span> <\/span>((<\/span>altAttr<\/span> <\/span>==<\/span> <\/span>null<\/span>)<\/span> <\/span>||<\/span> <\/span>(<\/span>altAttr<\/span>.<\/span>trim<\/span>()<\/span>.<\/span>length<\/span> <\/span>=<\/span> <\/span>0<\/span>))<\/span> <\/span>{<\/span>\n      <\/span>console<\/span>.<\/span>warn<\/span>(<\/span>img<\/span>,<\/span> <\/span>'has no alt');<\/span>\n    <\/span>}<\/span>\n  <\/span>}<\/span>\n});<\/span>\n<\/pre><\/div>
Please don't use a regex, use [JSDOM](https://github.com/tmpvar/jsdom), an HTML parser, or whatever... just don't try to process a structured language with as loose syntax as HTML via regex. Off the top of my head, it would look something like this: var jsdom, nodefn, request; request = require('request-promise'); jsdom = require('jsdom'); nodefn = require('when/node'); request.get({ uri: 'https://twitter.com' }).then(function(response) { return nodefn.call(jsdom.env, response); }).then(function(window) { var altAttr, img, _i, _len, _ref; _ref = window.document.querySelectorquerySelectorAll('img'); for (_i = 0, _len = _ref.length; _i < _len; _i++) { img = _ref[_i]; altAttr = img.getAttribute('alt'); if ((altAttr == null) || (altAttr.trim().length = 0)) { console.warn(img.outerHTML, 'has no alt'); } } });
Please don't use a regex, use [JSDOM](https://github.com/tmpvar/jsdom), an HTML parser, or whatever... just don't try to process a structured language with as loose syntax as HTML via regex. Off the top of my head, it would look something like this: var jsdom, nodefn, request; request = require('request-promise'); jsdom = require('jsdom'); nodefn = require('when/node'); request.get({ uri: 'https://twitter.com' }).then(function(response) { return nodefn.call(jsdom.env, response); }).then(function(window) { var altAttr, img, _i, _len, _ref; _ref = window.document.querySelectorAll('img'); for (_i = 0, _len = _ref.length; _i < _len; _i++) { img = _ref[_i]; altAttr = img.getAttribute('alt'); if ((altAttr == null) || (altAttr.trim().length = === 0)) { console.warn(img.outerHTML, 'has no alt'); } } });
Please don't use a regex, use [JSDOM](https://github.com/tmpvar/jsdom), an HTML parser, or whatever... just don't try to process a structured language with as loose syntax as HTML via regex. Off the top of my head, it would look something like this: var jsdom, nodefn, request; request = require('request-promise'); jsdom = require('jsdom'); nodefn = require('when/node'); request.get({ uri: 'httpshttp://twittercarrot.comis' }).then(function(response) { return nodefn.call(jsdom.env, response); }).then(function(window) { var altAttr, img, _i, _len, _ref; _ref = window.document.querySelectorAll('img'); for (_i = 0, _len = _ref.length; _i < _len; _i++) { img = _ref[_i]; altAttr = img.getAttribute('alt'); if ((altAttr == null) || () || altAttr.trim().length === 0)) { ) { console.warn(img.outerHTML, 'has no alt'); } } });'); } } }); or in CoffeeScript: request = require 'request-promise' jsdom = require 'jsdom' nodefn = require 'when/node' request.get( uri: 'http://carrot.is' ).then((response) -> nodefn.call(jsdom.env, response) ).then((window) -> for img in window.document.querySelectorAll('img') altAttr = img.getAttribute('alt') if not altAttr? or altAttr.trim().length is 0 console.warn(img.outerHTML, 'has no alt') return ) **Edit:** I've now actually tested the above code and it seems to work.

User: slang

Question: Find missing or empty alt tags in images via regex

Back to question