Alan Han
Alan Han

Reputation: 960

remove certain words from paragraphs using jquery

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

Answers (3)

Esailija
Esailija

Reputation: 140210

$(".author").each( function(){
    var text = this.firstChild.nodeValue;
    this.firstChild.nodeValue = text.replace( /(^By|\bon\b)/g, "" );
});

Upvotes: 2

bigblind
bigblind

Reputation: 12867

$('.author').each(function(){
$(this).text($(this).text().replace(/on|by/g,""))
});

Upvotes: 9

dfsq
dfsq

Reputation: 193261

No need of additional each:

$("p.author").text(function() {
    return $(this).text().replace(/(By|on)/g, '');
});

Upvotes: 2

Related Questions