Javascript RegExp 匹配 <a> 标签之间的文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7864723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript RegExp match text between <a> tags
提问by Ovidiu Aleka
I need to match with a javascript RegExp the string: bimbo999 from this a tag: <a href="/game.php?village=828&screen=info_player&id=29956" >bimbo999</a>
我需要用一个 javascript RegExp 匹配字符串:bimbo999 来自这个标签: <a href="/game.php?village=828&screen=info_player&id=29956" >bimbo999</a>
The numbers from URL vars (village and id) are changing every time so I have to match the numbers somehow with RegExp.
来自 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&screen=info_player&id=29956" >bimbo999</a></th></tr>
<tr><td>Village:</td><td><a href="/game.php?village=828&screen=info_village&id=848" >bimbo999s village (515|520) K55</a></td></tr>
<tr><td>Origin of the troops:</td><td><a href="/game.php?village=828&screen=info_village&id=828" >KaLa I (514|520) K55</a></td></tr>
</table><br />
<h4>Units:</h4>
<table class="vis">
I tried with this:
我试过这个:
var match = h.match(/Supported player:</th>(.*)<\/a><\/th></i);
but is not working. Can you guys, help me?
但不工作。你们能帮帮我吗?
回答by zzzzBov
Try this:
尝试这个:
/<a[^>]*>([\s\S]*?)<\/a>/
<a[^>]*>
matches the openinga
tag([\s\S]*?)
matches any characters before the closing tag, as few as possible<\/a>
matches the closing tag
<a[^>]*>
匹配开始a
标签([\s\S]*?)
匹配结束标记之前的任何字符,尽可能少<\/a>
匹配结束标签
The ([\s\S]*?)
captures the text between the tags as argument 1 in the array returned from an exec
or match
call.
所述([\s\S]*?)
捕获作为在阵列中的参数1的标签之间的文本从返回exec
或match
呼叫。
This is really only good for findingtext 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.
这真的只适用于在元素中查找文本a
,它不是非常安全或可靠,但是如果您有一大页链接并且您只需要它们的文本,那么就可以了。
A much safer way to do this without RegExp would be:
没有 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;
}
回答by Al-Mothafar
I don't have experience with Regex, but I think you can use JQuery with .text()
!
我没有使用 Regex 的经验,但我认为您可以将 JQuery 与.text()
!
I mean if you use :
我的意思是如果你使用:
var hrefText = $("a").text();
You will get your text without using Regex!
您无需使用正则表达式即可获得文本!
.find("a")
and then gives you a list of a's tags objects and then use .each()
to loop on that list then you can get the text by using .text()
.
.find("a")
然后为您提供a的标签对象列表,然后用于.each()
在该列表上循环,然后您可以使用.text()
.
Or your can use a class selector, id or anything you want!
或者您可以使用类选择器、id 或任何您想要的东西!