简体   繁体   English

为什么这个正则表达式与结束>匹配

[英]Why does this regex match the closing >

For this regex: 对于这个正则表达式:

/<(\/?)(\w+)([^>]*?)>/

on this string: 在这个字符串上:

<div class='test'><b>Hello</b> <i>world!</i></div>

the match returned is: 返回的匹配是:

<div class='test'>

I am confused at the ([^>]*?) capture. 我对([^>]*?)捕获感到困惑。 Doesn't ^> means that it is the inverse of > ? 不是^>意味着它是>的倒数?

Here's the breakdown of that regex: 这是正则表达式的细分:

<(\\/?) matches the opening < , possibly followed by / <(\\/?)匹配开头< ,可能后跟/

(\\w+) matches many word chars (it's in parens so the capture group will contain the HTML element by itself) (\\w+)匹配许多单词字符(它在parens中,因此捕获组将自己包含HTML元素)

([^>]*?)> matches multiple characters (lazily) that are not > , followed by a > ([^>]*?)>匹配不是>多个字符(懒惰),后跟一个>

So in your example, it's correct that it matches <div class='test'> 所以在你的例子中,它匹配<div class='test'>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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