Javascript RegExp匹配< a>之间的文本标签 [英] Javascript RegExp match text between <a> tags

查看:83
本文介绍了Javascript RegExp匹配< a>之间的文本标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要匹配一个javascript RegExp字符串:bimbo999来自这个标签:< a href =/ game.php?village = 828& amp; screen = info_player& amp; id = 29956> bimbo999< / a>

I need to match with a javascript RegExp the string: bimbo999 from this a tag: <a href="/game.php?village=828&amp;screen=info_player&amp;id=29956" >bimbo999</a>

URL变量(村庄和身份证)的数字每次都在变化所以我必须用RegExp以某种方式匹配数字。

The numbers from URL vars (village and id) are changing every time so I have to match the numbers somehow with RegExp.

</tr>
                    <tr><td>Sent</td><td >Oct 22, 2011  17:00:31</td></tr>
                                <tr>
                        <td colspan="2" valign="top" height="160" style="border: solid 1px black; padding: 4px;">
                            <table width="100%">
    <tr><th width="60">Supported player:</th><th>
    <a href="/game.php?village=828&amp;screen=info_player&amp;id=29956" >bimbo999</a></th></tr>
    <tr><td>Village:</td><td><a href="/game.php?village=828&amp;screen=info_village&amp;id=848" >bimbo999s village (515|520) K55</a></td></tr>
    <tr><td>Origin of the troops:</td><td><a href="/game.php?village=828&amp;screen=info_village&amp;id=828" >KaLa I (514|520) K55</a></td></tr>
    </table><br />

    <h4>Units:</h4>
    <table class="vis">

我试过这个:

var match = h.match(/Supported player:</th>(.*)<\/a><\/th></i);

但不起作用。你们能帮助我吗?

but is not working. Can you guys, help me?

推荐答案

试试这个:

/<a[^>]*>([\s\S]*?)<\/a>/




  • < a [^>] *> 匹配开头 a 标签

  • ([\\\ S] *?)匹配结束标记前的任何字符,尽可能少

  • < \ / a> 匹配结束标记

    • <a[^>]*> matches the opening a tag
    • ([\s\S]*?) matches any characters before the closing tag, as few as possible
    • <\/a> matches the closing tag
    • ([\\\\ S] *?)将标记之间的文本捕获为从 exec 匹配调用返回的数组中的参数1。

      The ([\s\S]*?) captures the text between the tags as argument 1 in the array returned from an exec or match call.

      这对 a 元素中找到文本非常有用,它不是非常安全或可靠,但是如果你有一个很大的链接页面而你只需要他们的文本,那就可以了。

      This is really only good for finding text within a elements, it's not incredibly safe or reliable, but if you've got a big page of links and you just need their text, this will do it.

      如果没有RegExp,更安全的方法是:

      A much safer way to do this without RegExp would be:

      function getAnchorTexts(htmlStr) {
          var div,
              anchors,
              i,
              texts;
          div = document.createElement('div');
          div.innerHTML = htmlStr;
          anchors = div.getElementsByTagName('a');
          texts = [];
          for (i = 0; i < anchors.length; i += 1) {
              texts.push(anchors[i].text);
          }
          return texts;
      }
      

      这篇关于Javascript RegExp匹配&lt; a&gt;之间的文本标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆