匹配闭合标签

本文最后更新于:7 个月前

//匹配闭合标签

function eg(chars) {
    let r = /\<.+?\>/g;
    let res = chars.match(r);
    let x = [];
    let rr = /\<\//;
    let reg1 = /([^<]+)$/;
    let reg2 = /([^/]+)$/;
    for (let i = 0; i < res.length; i++) {
        if (rr.test(res[i])) {
            let temp = x.pop();
            let temp1 = temp.match(reg1, '')[0].match(/\w*/, '')
            let temp2 = res[i].match(reg2, '')[0].match(/\w*/, '')
            if (temp1[0] != temp2[0]) {
                return [false, temp1[0] + '没有闭合'];
            }
        } else {
            x.push(res[i]);
        }
    }
    return true;
}
let str = '<html lang="en"><head><meta charset="UTF-8"></meta><meta name="viewport" content="width=device-width initial - scale=1.0"></meta><title>Document</title></head><body><div><div><script src="20200901.js"></script></body></html>'
console.log(eg(str))


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!