【问题标题】:Search/Replace extract strings between elements搜索/替换元素之间的提取字符串
【发布时间】:2018-03-09 13:38:32
【问题描述】:

我正在尝试从一个长 xml 文件中提取所有 url 字符串,我需要的 url 位于 loc 元素之间,我想丢弃其他所有内容。

  <loc>https://www.website.com/1</loc>

所以例如我会这样:

  <url>
   <loc>https://www.website.com/1</loc>
   <lastmod>2017-04-01T08:18:42+00:00</lastmod>
   <changefreq>daily</changefreq>
   <priority>1.0000</priority>
  </url>

  <url>
   <loc>https://www.website.com/2</loc>
   <lastmod>2017-04-01T08:18:42+00:00</lastmod>
   <changefreq>daily</changefreq>
   <priority>1.0000</priority>
  </url>

  <url>
   <loc>https://www.website.com/3</loc>
   <lastmod>2017-04-01T08:18:42+00:00</lastmod>
   <changefreq>daily</changefreq>
   <priority>1.0000</priority>
  </url>

然后返回:

https://www.website.com/1
https://www.website.com/2
https://www.website.com/3

有什么想法吗?提前谢谢你

【问题讨论】:

标签: html regex xml replace atom-editor


【解决方案1】:
var regex = /https.*(?=<\/loc>)/gm;
str.match(regex)

这将返回一个包含所有匹配项的数组

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-27
    • 2021-12-05
    • 1970-01-01
    • 2010-11-14
    • 1970-01-01
    相关资源
    最近更新 更多