Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove certain words from paragraphs using jquery

Tags:

html

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!

like image 444
Alan Han Avatar asked Jan 23 '12 19:01

Alan Han


2 Answers

$('.author').each(function(){
$(this).text($(this).text().replace(/on|by/g,""))
});
like image 144
bigblind Avatar answered Nov 10 '22 14:11

bigblind


$(".author").each( function(){
    var text = this.firstChild.nodeValue;
    this.firstChild.nodeValue = text.replace( /(^By|\bon\b)/g, "" );
});
like image 40
Esailija Avatar answered Nov 10 '22 14:11

Esailija