简体   繁体   中英

RegEx in JavaScript: Content Between Two Tags

I need to extract the content between two XML tags, excluding the tags.

PS: I won't be using this just to parse XML. I'll be using the RegEx in JavaScript, so the lookbehind won't work.

What am I doing wrong?

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:

/(?:(?=(\<(?!\/)(.*?)\>)))([\s\S]*?)(?=(\<(?=\/)(.*?)\>))/igm

Results:

<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]

What I Want

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>

How about

<(\w+)[^>]+>\n*([\s\S]*)<\/\1>

It'll grab your tag, capture everything up to tag repeated prefixed with / .

Result in capture group 2.

Check it out here at regex101 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM