javascript 使用正则表达式如何多次匹配 XML 标签?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4341421/
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
With regex how do i match between an XML tag multiple times?
提问by Oscar Godson
First, before you say anything, i HAVE to do this because the RSS is malformed, but i can't correct it on my end. So, while I tried using an RSS and a XML parser, they fail and i only have front end access. However, i'm super close, but i can't figure out why this wont match.
首先,在你说什么之前,我必须这样做,因为 RSS 格式错误,但我无法纠正它。所以,当我尝试使用 RSS 和 XML 解析器时,它们失败了,我只能访问前端。但是,我非常接近,但我不知道为什么这不匹配。
The feed (it's a long1 line string): http://pastebin.com/5dJhXCvf
提要(这是一个很长的1 行字符串):http: //pastebin.com/5dJhXCvf
First Example:
第一个例子:
<title>(.+)</title>
This i thought worked great with my test of just:
我认为这对我的测试非常有效:
<title>"cterrorism task force" location:oregon - Google News</title>
But the issue is that it matches everything then as one match for example:
但问题是它将所有内容都匹配为一个匹配项,例如:
<title>"cterrorism task force" location:oregon - Google News</title><title>"cterrorism task force" location:oregon - Google News</title>
Equals 1 result item in my array from exec()and match()
等于我的数组中的 1 个结果项来自exec()和match()
So i tried:
所以我试过:
<title>([\w\d\s\=\%\_\`\~\+\!\@\#$\%\^\&\*\(\)\:\'\"\[\]\{\}\|\,\.\/]+)</title>
But that returns nothing... Any ideas?
但这没有任何回报......有什么想法吗?
回答by detunized
回答by Laurence Gonsalves
The RSS you posted is well-formed XML, but not valid RSS (according to the W3C feed validator). Since it's well-formed your best bet is still to use an XML parser, not to use a regex. In fact, most RSS parsers should be ok too, as RSS is kind of notorious for having validation issues (partly due to poor specifications early on), so any RSS parser worth using shouldn't have any trouble with the kinds of validation problems the W3C validator is reporting.
您发布的 RSS 是格式良好的 XML,但不是有效的 RSS(根据 W3C 提要验证器)。由于它格式良好,因此您最好的选择仍然是使用 XML 解析器,而不是使用正则表达式。事实上,大多数 RSS 解析器也应该没问题,因为 RSS 因存在验证问题而臭名昭著(部分原因是早期的规范不佳),因此任何值得使用的 RSS 解析器都不会遇到验证问题的种类W3C 验证器正在报告。
As an aside, that looks like a Google News feed. You can get valid Atom by changing the output parameter from "rss" to "atom". eg:
顺便说一句,这看起来像一个谷歌新闻提要。您可以通过将输出参数从“rss”更改为“atom”来获得有效的 Atom。例如:
http://news.google.com/news?pz=1&cf=all&ned=us&hl=en&topic=h&num=3&output=atom
Google's services that generate feeds generally do a better job at producing Atom rather than RSS. That said, you may also want to report the invalid RSS to Google.
Google 的生成提要的服务通常在生成 Atom 而非 RSS 方面做得更好。也就是说,您可能还想向 Google 报告无效的 RSS。
回答by Mike Clark
Try a lazy quantifier:
尝试一个懒惰的量词:
<title>([^<]+?)</title>
回答by Hamish
Try an ungreedy expression by adding the U flag:
通过添加 U 标志尝试非贪婪表达式:
"/<title>(.+)</title>/U"
This tells it to match on the smallest match rather than the largest match available.
这告诉它匹配最小的匹配而不是可用的最大匹配。
回答by Mark Thomas
Many parsers can handle slight deviations from the specs. Any binding to the excellent libxml2library would be able to handle poorly formed XML. There are bindings in many languages. For example, the following Ruby snippet parses it just fine:
许多解析器可以处理与规范的轻微偏差。任何与优秀的libxml2库的绑定都能够处理格式不佳的 XML。许多语言都有绑定。例如,下面的 Ruby 代码片段可以很好地解析它:
require 'nokogiri'
xml = open('rss.txt').read
doc = Nokogiri::XML.parse(xml)
doc.xpath('//title').each do |title|
puts title.inner_text
end
Result:
结果:
"joint terrorism task force" location:oregon - Google News
"joint terrorism task force" location:oregon - Google News
Federal and FBI Joint Terrorism Task Force are still flawed - OregonLive.com
Striking a fair balance - OregonLive.com
Blame the terrorists, not the FBI - Portland Tribune
Why Oregon? Why not?: Terrorism can strike anywhere - The Register-Guard
INDIVIDUAL TRAVEL UNDER ATTACK - NewsWithViews.com
The other terrorism-and pondering Portland - BlueOregon
Fla. dance troupe causes scare at Lincoln Tunnel - Northwest Cable News
Edit: based on your comments I see you're using jQuery. You should be able to use a jQuery XML parser to extract the titles (and other parts, as needed).
编辑:根据您的评论,我看到您正在使用 jQuery。您应该能够使用 jQuery XML 解析器来提取标题(以及其他部分,根据需要)。

