Skip to content

Instantly share code, notes, and snippets.

@pankajladhar
Created January 13, 2018 08:08
Show Gist options
  • Save pankajladhar/9230b3423a06e25c11f0847ebcc3f195 to your computer and use it in GitHub Desktop.
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"
/*
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