我需要檢查具有多行的字串是否包含任何 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 標簽檢查。
uj5u.com熱心網友回復:
您可以使用
/^(?![^]*<[^>] >)[^]*/.test(text)
詳情:
^- 字串的開始(?![^]*<[^>] >)- 緊靠右邊,不應有零個或多個字符后跟<,一個或多個字符,而不是>一個>字符。[^]*- 盡可能多的任何零個或多個字符
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/452292.html
