简体   繁体   English

如何使用javascript获取字符串中重复的DYNAMIC html标签之间的字符串? (没有正则表达式,除非它是唯一的方法!)

[英]How can I get the string between repeated DYNAMIC html tags in a string using javascript? (No regex unless its the only way!)

As a mean to explain I am not posting the gigantic string I have (which contains dynamic html tags and are auto-generated with unique id).作为解释的一种方式,我没有发布我拥有的巨大字符串(其中包含动态 html 标签并使用唯一 id 自动生成)。 I want to get the text between every tag that says "<p class=partial_entry>... comment...</p>" using anything excep Regex , and in my case ill have a string of 20 repeated tags by getting the outerHTML.我想使用任何 excep Regex来获取每个标记之间的文本"<p class=partial_entry>... comment...</p>" ,在我的情况下,通过获取 20 个重复标记的字符串外部HTML。 I have an example below:我在下面有一个例子:

var str = "<div class=prw_rup prw_reviews_text_summary_hsx data-prwidget-name=reviews_text_summary_hsx data-prwidget-init=handlers>
<div class=entry><p class=partial_entry>You have to try their special sushi rolls like acevichado or patrullero. They have great selections of sushi and other dishes.</p>
</div></div><div class=prw_rup prw_reviews_text_summary_hsx data-prwidget-name=reviews_text_summary_hsx data-prwidget-init=handlers><div class=entry><p class=partial_entry>
All you can eat sushi fir $20 ($24 including tax)! Christian, our server, was wonderful and attentive.
</p></div></div><div class=prw_rup prw_reviews_text_summary_hsx data-prwidget-name=reviews_text_summary_hsx data-prwidget-init=handlers>
<div class=entry><p class=partial_entry>The place was good, also the waiters, but definitely sushi is the best in town 
for my opinion, even with the few options of it in this place. I will be there soon again.</p></div></div>";

What I want is the 3 comments I have in my example so I use the code for 20:我想要的是我在示例中的 3 条评论,所以我使用了 20 条代码:

- You have to try their special sushi rolls like acevichado or patrullero. They have great selections of sushi and other dishes.

- All you can eat sushi fir $20 ($24 including tax)! Christian, our server, was wonderful and attentive.

- The place was good, also the waiters, but definitely sushi is the best in town for my opinion, even with the few options of it in this place. I will be there soon again.

I tried making my own code but the text or tags in between are not taken off since it detects the tag I preset, example: "THE WHOLE STRING AFTER THE TAG IM LOOKING FOR + <p class=partial_entry>... comment...</p>" and I only want ...comment... part.我尝试制作自己的代码,但中间的文本或标签没有被删除,因为它检测到我预设的标签,例如: "THE WHOLE STRING AFTER THE TAG IM LOOKING FOR + <p class=partial_entry>... comment...</p>"整个"THE WHOLE STRING AFTER THE TAG IM LOOKING FOR + <p class=partial_entry>... comment...</p>"而我只想要...comment...部分。

The code I made is below:我制作的代码如下:

var temp = "<p class=partial_entry";
    var res = str.split('>');
    var res2 = res.indexOf(temp) + 1;
    var resultado = null;
    
     if (res2 < res.length && res2 != -1) {
         resultado = res[ res2 ]; // gets the next one
    }
    
    alert(resultado);

This is pretty trivial, assuming the comments will always be wrapped in a node with class partial_entry :这非常简单,假设评论将始终包含在具有partial_entry类的节点中:

var commentNodes = document.getElementsByClassName('partial_entry');
var comments = [];
for (var i = 0; i < commentNodes.length; i++) {
  comments.push(commentNodes[i].innerText);
}

You could use a for loop that loops through every single character until it sees , then stores every character until it sees您可以使用 for 循环遍历每个字符直到它看到,然后存储每个字符直到它看到

It probably isn't the best way, and won't work if there's other tags inside the p. 这可能不是最好的方法,如果 p 中有其他标签,它也不会起作用。 But it works. 但它有效。

 var text = "<p class=partial_entry>This text should show up</p>" var seenBeginTag = false; var seenEndTag = false; var output = []; for (var i = 0; i < text.length; i++) { if (seenBeginTag && seenEndTag) { if (text[i] == '<') { seenBeginTag = false; seenEndTag = false; } else { output.push(text[i]); } } else if (seenBeginTag) { if (text[i] == '>') { seenEndTag = true; } } else if (text[i] == '<') { if (text[i+1] == 'p') { seenBeginTag = true; } } } console.log(output.join(''));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Javascript 在字符串中找到类似 HTML 的标签? - How can I find HTML like tags in a string using Javascript? 正则表达式:如何在忽略 HTML 标签的 HTML 字符串中获得最后一个词? - Regex: How can I get the last word in an HTML string ignoring HTML tags? 给定一个字符串,如何使用 JavaScript 删除除特定“标签”(及其“子项”)之外的所有“HTML 标签”? - Given a string, how can I use JavaScript to remove all 'HTML tags' except a specific 'tag' (and its 'children')? 如何使用javascript删除字符串中的html标记 - How to remove only html tags in a string using javascript 如何在 javascript 中使用正则表达式从另一个字符串中获取特定字符串 - How can i get particular string from another string using regex in javascript 使用正则表达式javascript从字符串中删除HTML标签和空格 - Remove HTML Tags and whitespace from string using regex javascript 如何使用 jQuery 从字符串中删除输入标签? 该字符串还包含其他 html 标签 - How can I remove input tags from a string using jQuery? This string contains other html tags as well Javascript RegEx匹配HTML标签内的字符串 - Javascript RegEx matching a string inside HTML tags 如何仅获取html标签? - How can I get only tags of html? 如何使用正则表达式从 javascript 中的 HTML 字符串中删除所有时间戳? - How can I remove all timestamps from HTML string in javascript, using regex?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM