While it is possible, and generally suggested, to get attributes without using regex, I've created one that will attempt to pull all attributes from an html tag string.
var string = '<a href="next.html" title="\'Next\' >>" target="_self" onclick="var target=\'_blank\'; window.open(this.href + \"?test=1\", target); return false;">Next ></a>';
var regex = new RegExp('[\\s\\r\\t\\n]*([a-z0-9\\-_]+)[\\s\\r\\t\\n]*=[\\s\\r\\t\\n]*([\'"])((?:\\\\\\2|(?!\\2).)*)\\2', 'ig');
var attributes = {};
while ((match = regex.exec(string))) {
attributes[match[1]] = match[3];
}
Outputs:
{
href: "next.html",
onclick: "var target='_blank'; window.open(this.href + \"?test=1\", target); return false;",
target: "_self",
title: "'Next' >>"
}
It works by expecting an equals sign that follows letters, numbers, underscores and hyphens, and is also followed by either a quotation mark or apostrophe. It'll use the match for the apostrophe/quotation mark to determine when the attribute ends, in case the attribute also contains other apostrophe or quotation marks.
I've tried to account for possible line breaks and spacing, however, I've still found some edge cases where it will have issues where the attribute contains an equals signs.
EDIT
Adjusted the above code to double escape whitespace and lines, as well as fix issues with nested equals signs = and escaped quotes \" and apostrophes \'.
$("meta[property='og:title']").attr('content')?