我将使用什么JavaScript正则表达式来发现字符串包含打开的HTML标记,但不包含关闭的HTML标记

What JavaScript regular expression would I use to find out that a string contains an open HTML tag but not a close HTML tag?

本文关键字:标记 HTML 包含关 包含打 字符串 什么 JavaScript 发现 正则表达式      更新时间:2023-09-26

我将使用什么JavaScript正则表达式来发现字符串包含一个打开的HTML标记而不是一个关闭的HTML标记。例如:

var tags = ['h1', 'div', 'span']; // tag to look for
var str1 = 'lorem ipsum <h1>hello world'; // here it is!
var str2 = 'boo foo 123 test'; // this one doesn't have any
var str3 = '<span>boo-boo</span>'; // this has the tags but it is not the case as we only need the ones that have open tags and not close tags.

你不能用正则表达式解析HTML。跟踪打开/关闭标记对需要堆栈,而正则表达式是有限状态机,它没有堆栈。

'('<(/?[^'>]+'>)'