Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jeffdrumgod/5818702 to your computer and use it in GitHub Desktop.
Save jeffdrumgod/5818702 to your computer and use it in GitHub Desktop.
function getTagFromHtmlText(html, tagFind, attrFind, attrValueFind, returnResult, all){
html = html || '',
reg = '';
if(all === undefined){
all = false;
}
html = html.replace(/<!--[\s\S]*?-->/g, ''); // remove comments
if(attrFind === undefined || attrFind === false){
reg = new RegExp('<'+tagFind+'\\b[^>]*(\\1)([^>]*/>|[^>]*>((?:(?:(?!<'+tagFind+'[^>]*>|<\/'+tagFind+'>)[\\s\\S])+|<'+tagFind+'[^>]*>([\\s\\S]*?)<\/'+tagFind+'>)*)<\/'+tagFind+'>)','gi');
}else{
reg = new RegExp('<'+tagFind+'\\b[^>]*'+attrFind+'=([\\\'"])?.*'+attrValueFind+'.*(\\1)([^>]*/>|[^>]*>((?:(?:(?!<'+tagFind+'[^>]*>|<\/'+tagFind+'>)[\\s\\S])+|<'+tagFind+'[^>]*>([\\s\\S]*?)<\/'+tagFind+'>)*)<\/'+tagFind+'>)','gi');
}
var matches = html.match(reg);
if(matches === null){
return false;
}else{
if(returnResult === true){
if(all){
return matches;
}else{
return matches[0];
}
}
return true;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment