更新时间:2022-06-14 21:20:21
在您的示例中,您可以使用:
In your example you can use:
/
<h // Match literal <h
(.) // Match any character and save in a group
> // Match literal <
.*? // Match any character zero or more times, non greedy
<\/h // Match literal </h
\1 // Match what previous grouped in (.)
> // Match literal >
/g
var str = '<h1>A</h1><h2>B</h2><p>Foobar</p><h3>C</h3>'
str.match(/<h(.)>.*?<\/h\1>/g); // ["<h1>A</h1>", "<h2>B</h2>", "<h3>C</h3>"]
但请不要使用regexp解析HTML,请阅读 RegEx匹配除XHTML自包含标签之外的开放标签
But please don't parse HTML with regexp, read RegEx match open tags except XHTML self-contained tags