Skip to content

Instantly share code, notes, and snippets.

@jstrimpel
Created November 12, 2013 17:06
Show Gist options
  • Save jstrimpel/7434663 to your computer and use it in GitHub Desktop.
Save jstrimpel/7434663 to your computer and use it in GitHub Desktop.
I suck at regexes. I know there has to better regex for extracting all the values for a particular attribute from an of HTML string.
function getAttributeValues(html) {
var htmlSubstr = html,
match = true,
values = [],
start = 0;
while (match) {
htmlSubstr = htmlSubstr.substr(start);
match = htmlSubstr.match(/<[^>]*\s(?:some-attribute-name=["']([^"']*)["'])[^>]*>/);
if (match) {
values.push(match[1]);
start = match[0].length + match.index;
}
}
return values;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment