Skip to content

Instantly share code, notes, and snippets.

View eXist-FraGGer's full-sized avatar
Working

Ivan Sarokin eXist-FraGGer

Working
View GitHub Profile
@eXist-FraGGer
eXist-FraGGer / checkBrackets.js
Created June 12, 2018 19:54
Method for checking brackets in mathematical string
function checkBrackets(text) {
const stackBrackets = [];
const strBrackets = text.replace(/[^()[\]]/g, '');
for (let i = 0; i < strBrackets.length; i++) {
if (~[ '(', '[' ].indexOf(strBrackets[ i ])) {
stackBrackets.push(strBrackets[ i ]);
} else if ((stackBrackets[ stackBrackets.length - 1 ] === '(' && strBrackets[ i ] === ')') ||
(stackBrackets[ stackBrackets.length - 1 ] === '[' && strBrackets[ i ] === ']')) {
stackBrackets.pop();