Created
January 13, 2018 08:08
-
-
Save pankajladhar/9230b3423a06e25c11f0847ebcc3f195 to your computer and use it in GitHub Desktop.
ExpressionMatcher is take the str parameter being passed and return "Expression Matched" or "Expression Not Matched"
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
/* | |
ExpressionMatcher is take the str parameter being passed | |
and return "Expression Matched" or "Expression Not Matched" | |
@param {string} str | |
@param {array} openExpArr <optional> | |
default value ['[', '{', '('] | |
@param {array} closeExpArr <optional> | |
default value [')', '}', ']'] | |
@returns | |
*/ | |
const ExpressionMatcher = (str, openExpArr, closeExpArr) => { | |
const openingExp = openExpArr || ['[', '{', '(']; | |
const closingExp = closeExpArr || [')', '}', ']']; | |
const inputExp = str.split(''); | |
const tempArr = []; | |
let index = 0 | |
while (index < inputExp.length) { | |
if (openingExp.indexOf(inputExp[index]) > -1) tempArr.push(inputExp[index]) | |
if (closingExp.indexOf(inputExp[index]) > -1) tempArr.pop() | |
index++ | |
} | |
return tempArr.length === 0 ? 'Expression Matched' : 'Expression Not Matched' | |
} | |
console.log(ExpressionMatcher("[{}]")) // Expression Matched | |
console.log(ExpressionMatcher("[{]")) // Expression Not Matched | |
console.log(ExpressionMatcher("$%", ['$'], ['%'])) // Expression Not Matched |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment