JavaScript中的RegEx:两个标签之间的内容



我需要提取两个XML标记之间的内容,不包括标记。

PS:我不会仅仅用它来解析XML。我将在JavaScript中使用RegEx,所以后备功能不起作用

我做错了什么?

XML:

<location maps="">
    RewriteMap map txt:map.txt
    RewriteMap lower int:tolower
    RewriteCond %{REQUEST_URI} ^/([^/.]+).html$ [NC]
    RewriteCond ${map:${lower:%1}|NOT_FOUND} !NOT_FOUND
    RewriteRule .? /index.php?q=${map:${lower:%1}} [NC,L]
</location>

RegEx:

/(?:(?=(<(?!/)(.*?)>)))([sS]*?)(?=(<(?=/)(.*?)>))/igm

结果:

<location maps="">
    RewriteMap map txt:map.txt
    RewriteMap lower int:tolower
    RewriteCond %{REQUEST_URI} ^/([^/.]+).html$ [NC]
    RewriteCond ${map:${lower:%1}|NOT_FOUND} !NOT_FOUND
    RewriteRule .? /index.php?q=${map:${lower:%1}} [NC,L]

我想要什么

RewriteMap map txt:map.txt
RewriteMap lower int:tolower
RewriteCond %{REQUEST_URI} ^/([^/.]+).html$ [NC]
RewriteCond ${map:${lower:%1}|NOT_FOUND} !NOT_FOUND
RewriteRule .? /index.php?q=${map:${lower:%1}} [NC,L]

您还可以使用以下正则表达式:(如果标记名是常量)

<location[^>]*>([^<]+)</location>

怎么样

<(w+)[^>]+>n*([sS]*)</1>

它会抓取您的标签,捕捉到以/为前缀重复的标签上的所有内容。

结果为捕获组2。

在regex101上查看它。

最新更新