我如何使用拆分但跳过html / javascript / php和其他内部标签?

时间:2015-11-06 21:42:28

标签: javascript php jquery html

我的代码在博客文章的X个单词之后插入HTML内容。代码有效,但是有一个问题:它会删除它在路上找到的所有东西,甚至是javascript,html等等。

if (index == 2)计算代码的前两个单词,然后在这些单词之后插入HTML(在本例中为图像),但它不区分html或纯文本。 我在这里找到一个主题,说我应该使用这样的东西:

result = subject.match(/<\s*(\w+\b)(?:(?!<\s*\/\s*\1\b)[\s\S])*<\s*\/\s*\1\s*>|\S+/g);

但我不知道如何实施它。

基本上,我需要代码来计算每个单词,但跳过任何标签,例如<\ " " ? --> <!-- />

小提琴:https://jsfiddle.net/kvenmL07/

HTML:

 <div style="width:450px; margin-left:auto; margin-right:auto" class="newsitem_text">
    <div style="width:350px; margin-left:auto; margin-right:auto">

    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec pellentesque urna eu pulvinar maximus. Sed elit nunc, vestibulum ut eros vitae, pellentesque rhoncus ipsum. In et metus non diam porttitor maximus iaculis nec lectus. Quisque sodales scelerisque auctor. Nam rutrum venenatis eros, eu condimentum erat placerat ut. Pellentesque sed tempus sem, eu viverra ipsum. Vestibulum nec turpis convallis, dapibus massa vitae, posuere mauris. Suspendisse mattis tincidunt lorem. Aliquam erat volutpat. Nullam at tincidunt erat, maximus laoreet ipsum.

    Quisque nunc neque, semper tincidunt placerat eget, blandit a ante. Suspendisse pulvinar, velit eu ultrices pulvinar, lacus sapien tincidunt ipsum, eget sollicitudin mauris eros molestie ex. Etiam quis orci dui. Phasellus vestibulum mollis molestie. Nam condimentum ornare nisl, sed finibus risus tempus vel. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Interdum et malesuada fames ac ante ipsum primis in faucibus.

    Vestibulum eget ullamcorper lorem. Aliquam mollis elit in sem dapibus dapibus. Proin vel massa a arcu dictum tincidunt in ut ante. Sed feugiat tempus dictum. Praesent in leo ullamcorper, sodales turpis et, vehicula tellus. Duis pellentesque dui ac turpis tristique imperdiet. Sed sed orci lectus.

    Suspendisse non egestas sem, sed tincidunt sem. Etiam laoreet dui sem. Mauris hendrerit massa tempus, euismod arcu sit amet, eleifend quam. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Phasellus id fringilla mauris. Cras dapibus non lacus at finibus. Nullam vitae sagittis neque. Mauris libero velit, interdum non vehicula non, lacinia non augue. Maecenas elementum lacinia interdum.

    Morbi eget mollis nisl. Integer accumsan condimentum tellus, lacinia pellentesque urna volutpat a. Nullam semper sem et erat commodo sollicitudin. Proin rhoncus felis eu aliquam venenatis. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Nulla pretium velit eu molestie condimentum. Vestibulum vitae velit mi. Integer nec leo quam. Nam pulvinar ligula congue consectetur tristique. Donec placerat faucibus diam sit amet fermentum. Ut id pellentesque risus. Nunc lacus orci, rhoncus ut risus sed, mattis posuere tellus. Nulla pellentesque eros sed neque consectetur dictum.</div></div>

Jquery的:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

<script type="text/javascript">
 jQuery(function($) {

var wordList = $(".newsitem_text").html().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);

});


</script>

2 个答案:

答案 0 :(得分:2)

如果你使用.text()而不是.html(),它将不会显示任何标签..例如:

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

然后

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

mytext等于“这是一个测试”;

答案 1 :(得分:0)

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

可能你只需要第一行。 $ each之后加上这个。行和newHtml + =行之前。

-----------------编辑

可能我第一次误解了它。尝试在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);

});