正则表达式查找多行字符串是否没有 html 标签

Regex to find whether the multiline String is without html tags or not

我需要检查多行字符串中是否包含任何 HTML 标签(<>)。

var regex= /^(?!.*<[^>]+>).*$/;


console.log('istextWithoutHtml--', regex.test('Line1\nLine2'));\ Expecting true, since it doesnt have html tags

\ Expecting false for these combinations, since it contains html tags in it
\ 'Line1<a>\nLine2'
\ 'Line1\nLine2<p>'
\ '<a></a>'
\ '<\img>'
\ '</a>\n</b>'

试用 1

var regex1 = new RegExp(regex);
    
console.log('istextWithoutHtml---', regex1.test('Line1\nLine2')); \ false (I am expecting true here)

console.log('istextWithoutHtml---', regex1.test('Line1<a>\nLine2')); \ false

试用 2

var regex2 = new RegExp(regex, 's');

console.log('istextWithoutHtml---', regex2.test('Line1\nLine2')); \ true
console.log('istextWithoutHtml---', regex2.test('Line1<a>\nLine2')); \ true (I am expecting false here)

试用 3

var regex3 = new RegExp(regex, 'm');

console.log('istextWithoutHtml---', regex3.test('Line1\nLine2')); \ true
console.log('istextWithoutHtml---', regex3.test('Line1<a>\nLine2')); \ true (I am expecting false here)

有没有办法在多行字符串中同时实现 HTML 标签检查。

您可以使用

/^(?![^]*<[^>]+>)[^]*/.test(text)

详情:

  • ^ - 字符串开头
  • (?![^]*<[^>]+>) - 紧靠右边,不应有零个或多个字符后跟 <,除 > 以外的一个或多个字符,然后是 > 字符。
  • [^]* - 尽可能多的任意零个或多个字符