JSFiddle - React, Tailwind, and code Playground
JavaScript
console.clear()
console.info(1,js("function test(){ alert(); }"));
console.info(2,js("function test(){ alert([]); }"));
console.info(3,js("function test)({ al]ert(); }"));
console.info(4,js("function test(){ alert(]); }"));
console.info(5,js("function test(){ alert(){]; }"));
console.info(6,js("function test()[ aler(t();} }"));
console.info(7,js("fun}ction test(){ alert(); }"));
function js(text){
var br_opn = '({[<',
br_cls = ')}]>'
var i, l=text.length, char, last, stack=[], bcc
for (i=0; i<l; i++) {
char = text[i];
if (br_opn.indexOf(char) != -1) {
// it's opening bracket
stack.push(char); // save it in stack
last = char;
} else {
bcc = br_cls.indexOf(char)
if (bcc != -1) { // closing bracket
if (!last) return false; // no pair
if (bcc != br_opn.indexOf(last)) return false;
// have an opening pair
stack.pop();
last = stack[stack.length - 1];
}
}
}
return stack.length==0;
}