gpt4 book ai didi

javascript - 如何使用 split 但跳过 html/javascript/php 和其他内部标签?

转载 作者:行者123 更新时间:2023-11-30 16:29:18 25 4
gpt4 key购买 nike

我的代码在博客文章的 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);

但是我不知道怎么实现。

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

fiddle :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>

J查询:

<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>

最佳答案

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

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

然后

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

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

关于javascript - 如何使用 split 但跳过 html/javascript/php 和其他内部标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33576169/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com