Home > Blockchain >  How to find the match or shuffled combination in JS
How to find the match or shuffled combination in JS

Time:10-02

I would like to find the match as following in the string:

  1. s = '()' -> result should be true

  2. s = '(]' -> result should be false

  3. s = '()[]{}' result should be true

  4. s = '([])' result should be true

  5. 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 (?:...)

Regex demo

CodePudding user response:

Use a regex pattern to match your requirements. Example Here

is a playground to test and check.

  • Related