JSFiddle - React, Tailwind, and code Playground

by no2don

JavaScript

function CheckUncloseTag(str) {
  str = str.toLowerCase();
  var tags = ["a","label", "span", "div", "ul", "li", "h1", "h2", "h3", "h4", "h5", "h6", "p", "table", "tr", "td", "b", "i", "u","embed","script","video"];
  var mismatches = [];
  tags.forEach(function(tag) {
    var pattern_open = '<' + tag + '( |>)';
    var pattern_close = '</' + tag + '>';

    var diff_count = (str.match(new RegExp(pattern_open, 'g')) || []).length - (str.match(new RegExp(pattern_close, 'g')) || []).length;

    if (diff_count != 0) {
      mismatches.push("Open/close mismatch for tag " + tag + ".");
    }
  });

  return mismatches;
}


var strError="您好這是我的測試字串<div>ABCEF<br><a>FEGHI<img><br/></img><p>JKLMN</p></p></div>結束結束";
var strCorrect="您好這是我的測試字串<div>ABCEF<br><a>FEGHI</a><img><br/></img><p>JKLMN</p></div>結束結束"

console.log(CheckUncloseTag(strError)); //["Open/close mismatch for tag a.", "Open/close mismatch for tag p."]
console.log(CheckUncloseTag(strCorrect)); //[]