JSFiddle - React, Tailwind, and code Playground

by Alexander

JavaScript

console.clear();

function verify(str) {

	let brackets = { "{" : "}", "(" : ")", "[" : "]", "<" : ">" },
      reg = /[{([<]/g,
      opening;
      
  while(reg.exec(str)) opening = reg.lastIndex -1;

  if(opening >= 0) {
		let closing = str.indexOf(brackets[str.charAt(opening)], opening);
		if(closing > 0)
    	return verify(str.slice(0, opening).concat(str.slice(closing+1)));
    return 1;
  }

  return 0;
};

    console.log( verify("<") === 1 );
    console.log( verify("---(++++)----") === 0 );
    console.log( verify("") === 0 );
    console.log( verify("before ( middle []) after ") === 0 );
    console.log( verify(") (") === 1 );
    console.log( verify("} {") === 1 );
    console.log( verify("<(   >)") === 1 );
    console.log( verify("(  [  <>  ()  ]  <>  )") === 0 );
    console.log( verify("   (      [)") == 1 );