Skip to content Skip to sidebar Skip to footer

How Can I Use Split But Skip Html/javascript/php And Other Inner Tags?

My code inserts a HTML content after X number of words of a blog post. The code works, but there is a problem: It strips everything it finds on the way, even javascript, html, what

Solution 1:

If you use .text() instead of .html() it will not show any of the tags.. for instance:

    <div id="test" class="test2">
       <span>this is a test</span>
    </div>

then

var mytext = $("#test").text();

mytext would equal "this is a test";

Solution 2:

word = word.replace(/<\/?[\w#"'-=:; {},.\r\n]+\/?>/g, '\n');
word = word.replace(/&nbsp;/gi, '');

probably you only need the first line. add this after the $each. line and before newHtml += line.

----------------- edit

Probably I misunderstood it first time. Try to remove tags first before split()

jQuery(function($) {

    //var wordList = $(".newsitem_text").html().split(' ');var wordList = $(".newsitem_text").html();
    wordList = wordList.replace(/<\/?[\w#"'-=:; {},.\r\n]+\/?>/g, '\n');
    wordList = wordList.split(' ')

    var newHtml = ' ';

    $.each(wordList, function(index, word){
      newHtml += ' ' + word;
      if (index == 2) {
       newHtml += '<img src="https://www.google.com.br/logos/doodles/2015/adolphe-saxs-201st-birthday-6443879796572160.2-res.png" />'
      }        
    })
    ;

    $(".newsitem_text").html(newHtml);

});

Post a Comment for "How Can I Use Split But Skip Html/javascript/php And Other Inner Tags?"