By ... on Jan 23, 2012

(...">

Remove specific 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!

+6
source share
3 answers
 $(".author").each( function(){ var text = this.firstChild.nodeValue; this.firstChild.nodeValue = text.replace( /(^By|\bon\b)/g, "" ); }); 
+2
source
 $('.author').each(function(){ $(this).text($(this).text().replace(/on|by/g,"")) }); 
+8
source

No need for extra each :

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

Source: https://habr.com/ru/post/906693/


All Articles