I would like to find the match as following in the string:
s = '()' -> result should be
true
s = '(]' -> result should be
false
s = '()[]{}' result should be
true
s = '([])' result should be
true
s = '{([])}' result should be
true
const isValid = (s) => { //how to return the value? } console.log(isValide('[]') //should be true
CodePudding user response:
If there is no nested of the same brackets, you might use:
^(?:\([^()]*\)|\[[^\]]*]|{[^{}]*}) $
The pattern repeats 1 or more times matching from (...)
or [...]
or {...}
using a negated character class.
The ^
asserts the start of the string, the $
asserts the end of the string. All 3 bracket types are repeated 1 or more times in a grouping construct (?:...)
CodePudding user response:
Use a regex pattern to match your requirements. Example Here
is a playground to test and check.