I am trying to replace all occurrences of a keyword inside an html document except those that are found inside a button, anchor or image tag. I have created a fiddle with the code that I am working with:
Could I please have some help getting the correct regular expression to exclude occurrences found inside images, anchors and buttons?
jQuery(document).ready(function($) {
var keyword = 'ARC';
$('p, h1, h2, h3, h4, h5, h6, td, th, li').each(function() {
var src_str = $(this).html();
var pattern = new RegExp('(?<!<button.*?>|<img.*?\/?>|<a.*?>)' + keyword + '(?!\/?>|<\/a>|<\/button>)', "gi");
var match = pattern.exec(src_str);
if (match) {
$(this).html(src_str.replace(pattern, '<mark>' + keyword + '</mark>'));
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<h2>
About the ARC
</h2>
<p>
ARC activity & recreation center Columbia Parks And RecreationThe ARC is Columbia, Missouri’s popular health club, fitness center, and place to exercise and have fun. This 73,000 sq. ft. facility has an indoor leisure pool, gymnasium (including basketball,
volleyball, and pickleball), strength training equipment and weights, cardio equipment, indoor track, group exercise classes, and much more! <a href="arc-url.html">ARC Link</a> <button>ARC Link</button></p>
<p>
<img src="something.jpg" alt="ARC Logo">
</p>