首页 文章

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

提问于
浏览
-1

作为一个解释的意思我不发布我拥有的巨大字符串(其中包含动态html标签,并使用唯一ID自动生成) . 我希望在每个标记之间得到文本 "<p class=partial_entry>... comment...</p>" using anything excep Regex ,在我的情况下,通过获取outerHTML,我会得到20个重复标记的字符串 . 我有一个例子如下:

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>" ,我只想要 ...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);

2 回答

  • 1

    您可以使用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(''));
    
  • 4

    这是非常简单的,假设注释将始终包含在类 partial_entry 的节点中:

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

相关问题