[英]How can I get the string between repeated DYNAMIC html tags in a string using javascript? (No regex unless its the only way!)
作為解釋的一種方式,我沒有發布我擁有的巨大字符串(其中包含動態 html 標簽並使用唯一 id 自動生成)。 我想使用任何 excep Regex來獲取每個標記之間的文本"<p class=partial_entry>... comment...</p>"
,在我的情況下,通過獲取 20 個重復標記的字符串外部HTML。 我在下面有一個例子:
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>";
我想要的是我在示例中的 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.
我嘗試制作自己的代碼,但中間的文本或標簽沒有被刪除,因為它檢測到我預設的標簽,例如: "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...
部分。
我制作的代碼如下:
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);
這非常簡單,假設評論將始終包含在具有partial_entry
類的節點中:
var commentNodes = document.getElementsByClassName('partial_entry');
var comments = [];
for (var i = 0; i < commentNodes.length; i++) {
comments.push(commentNodes[i].innerText);
}
您可以使用 for 循環遍歷每個字符直到它看到,然后存儲每個字符直到它看到
這可能不是最好的方法,如果 p 中有其他標簽,它也不會起作用。 但它有效。 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.