I'm using this script to wrap a specific word with span tags. I need to exclude the replace though if the the word is an href attribute.
// Prevent orphaned words for strings with more than 3 words
$("p:not(.form-row),h1,h2,h3,h4,h5,h6").each(function (i, e) {
var text = $(e).html();
text = text.trim().split(' ');
if (text.length > 3) {
var lastWord = text.pop();
text = text.join(' ') + " " + lastWord;
$(e).html(text);
}
// Wrap all occurrences of word-123 with a span so it doesn't break to two lines
$(e).html( $(e).html().replace(/word-123/ig, '<span class="nowrap">$&</span>') );
});
When the word is in an href, the word in the href is getting wrapped breaking the href. So
Lorem ipsum dolor sit amet, <a href="word-123">consectetur</a> adipiscing elit.
Is rendering as
Lorem ipsum dolor sit amet, <a href="<span class="nowrap">word-123</span>">consectetur</a> adipiscing elit.