【问题标题】:Javascript RegEx: Get all text matches surrounded by some other text?Javascript RegEx:获取被其他文本包围的所有文本匹配?
【发布时间】:2009-07-24 18:09:39
【问题描述】:

在 JavaScript/JQuery 中,我想获取在其他文本之间看到的所有文本。例如,如果 HTML 文档有:

<b class="blah">Blah: Some Text 1</b>
<div id="foo"><b class="blah">Blah: Some Text 2</b>

我想要一个包含“Some Text 1”和“Some Text 2”的数组,因为它们都在“&lt;b class="blah"&gt;Blah:”和“&lt;/b&gt;”之间

【问题讨论】:

    标签: javascript regex


    【解决方案1】:

    既然您提到了 jQuery,只需选择所有正确的节点并检查它们的文本。如果需要,您可以在此处放置正则表达式,但这不是必需的。

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    <title>test</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></script>
    <script type="text/javascript">
    $( function(){
        var texts = [];
        $('b.blah').each( function()
        {
          var txt = $(this).text();
          if ( 0 == txt.indexOf( 'Blah:' ) )
          {
              texts.push( txt.substr( 6 ) );
          }
        } );
        alert( texts );
    });
    </script>
    
    </head>
    <body>
      <b class="blah">Blah: Some Text 1</b>
      <div id="foo"><b class="blah">Blah: Some Text 2</b>
      <div id="foo"><b class="blah">Some Text 3</b>
    </body>
    </html>
    

    或者用一串HTML

    $( function(){
      var htmlChunk = '<b class="blah">Blah: Some Text 1</b>\n'
        + '<div id="foo"><b class="blah">Blah: Some Text 2</b></div>\n'
        + '<div id="foo2"><b class="blah">Some Text 3</b></div>';
    
        var texts = [];
        $('b.blah', '<div>' + htmlChunk + '</div>').each( function()
        {
          var txt = $(this).text();
          if ( 0 == txt.indexOf( 'Blah:' ) )
          {
              texts.push( txt.substr( 6 ) );
          }
        } );
        alert( texts );
    });
    

    【讨论】:

    • 我忘记了这个解决方案。但是,我要解析的 html 存储为字符串(即我的 html sn-p 存储在 javascript 变量中)。 Jquery 可以像分析 html 文件一样分析字符串吗?
    • 是的 - 在上面添加。不确定对于非常大的 HTML 块的效率如何。
    【解决方案2】:

    这在 JS 中有点困难,因为没有方便的方法来检索一组全局的 paren 捕获。像这样的 hack 可能会奏效:

    var chunked = text.replace(/.*<b class="blah">(.*?)<\/b>/g, '$1|ARBITRARY_SEPARATOR|');
    var chunks = chunked.split(/|ARBITRARY_SEPARATOR|/);
    chunks.pop();
    

    【讨论】:

    • 出于某种原因,这只是弹出“|” (字符串中的最后一个字符)。我做错了吗?
    • 嗯。您是否有机会使用chunks.pop() 的结果?我的意思是你应该在你做pop()之后使用chunks中剩下的东西。
    【解决方案3】:

    此代码将生成一个数组,其中包含“&lt;b class="blah"&gt;Blah:”和“&lt;/b&gt;”之间的文本。 在此示例中为“某些文本 1”和“某些文本 2”

    var s = '<b class="blah">Blah: Some Text 1</b><div id="foo"><b class="blah">Blah: Some Text 2</b>';
    
    var regex = /<b class="blah">Blah: (.+?)<\/b>/gi;
    var result = [];
    var e;
    while (e = regex.exec(s))
    {
      result.push(e[1]);
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-19
      相关资源
      最近更新 更多