Created
June 12, 2018 19:54
-
-
Save eXist-FraGGer/4593dc5e1c7e9925bf6661bab892f72e to your computer and use it in GitHub Desktop.
Method for checking brackets in mathematical string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | |
| } else { | |
| return false; | |
| } | |
| } | |
| return !stackBrackets.length; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment