Reputation: 960
I have a web page like this:
<p class="author">By ... on Jan 23, 2012</p>
<p class="content">(some content)</p>
<p class="author">By ... on Jan 23, 2012</p>
<p class="content">(some content)</p>
<p class="author">By ... on Jan 23, 2012</p>
<p class="content">(some content)</p>
...
I would like to use jquery to remove the words "By" and "on" from p.author, the result would be:
<p class="author">... Jan 23, 2012</p>
<p class="content">(some content)</p>
...
Thanks!
Upvotes: 7
Views: 5787
Reputation: 140210
$(".author").each( function(){
var text = this.firstChild.nodeValue;
this.firstChild.nodeValue = text.replace( /(^By|\bon\b)/g, "" );
});
Upvotes: 2
Reputation: 12867
$('.author').each(function(){
$(this).text($(this).text().replace(/on|by/g,""))
});
Upvotes: 9
Reputation: 193261
No need of additional each
:
$("p.author").text(function() {
return $(this).text().replace(/(By|on)/g, '');
});
Upvotes: 2