gpt4 book ai didi

Javascript RegExp 匹配 标签之间的文本

转载 作者:数据小太阳 更新时间:2023-10-29 03:59:52 24 4
gpt4 key购买 nike

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

URL 变量(村庄和 ID)中的数字每次都在变化,所以我必须以某种方式将数字与 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);

但是没有用。你们能帮帮我吗?

最佳答案

试试这个:

/<a[^>]*>([\s\S]*?)<\/a>/
  • <a[^>]*>匹配开头 a标签
  • ([\s\S]*?)匹配结束标记之前的任何字符,尽可能少
  • <\/a>匹配结束标签

([\s\S]*?)捕获标签之间的文本作为从 exec 返回的数组中的参数 1或 match打电话。

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


没有 RegExp 的更安全的方法是:

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;
}

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com