I wanted to find all h2 tags on a page and then wrap first two words in a span tag. I have checked this stackoverflow question, but it didn't help me.
I am able to do as per this link and dd span to first 2 words, but the only issue is that if I have html inside h2 tag for eg I have hyperlink inside h2 tag.
Can anyone please help me how to make this code so as html is ignored and only first two words are considered?
Any help is really appreciated!
My Fiddle :
My JS :
jQuery(document).ready(function($){
jQuery('h2').html(function (i, html) {
return html.replace(/(\w+\s\w+)/, '<span>$1</span>')
});
});
My HTML :
<h2>
<a title="test post 2" href="#">
test text dolor sit enum
</a>
</h2>
<h2>lorem ipsum dolor sit enum</h2>