Skip to content

Instantly share code, notes, and snippets.

@kheniparth
Created November 12, 2019 17:10
Show Gist options
  • Save kheniparth/ab8b48871a4dd3c2d0bbe13610f77fea to your computer and use it in GitHub Desktop.
Save kheniparth/ab8b48871a4dd3c2d0bbe13610f77fea to your computer and use it in GitHub Desktop.
JavaScript - Find external link anchor tag and replace html content
var regex = /<a(.+?(?=href))href="((http|https):\/\/(?!www.techmuzz.com)[\w\.\/\-=?#]+)"(.*?)>(.*?)<\/a>/g;
var replaceAnchor = '<a$1href="$2"$4>$5 <img src="https://techmuzz.com/favicon.ico"></a>';
var htmlElement = jQuery('.entry-content');
var updatedHtml = htmlElement.html().replace(regex, replaceAnchor);
htmlElement.replaceWith(updatedHtml);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment