0

I am working with a token on javascript and I wonder if anyone knows a way for knowing if the next token is one closing a parenthesis.

For example: Let's say we have the string "$(var8(today())) - $(storm())"

How can I know that the parenthesis that is between $ and var8 is being closed?

I could not found any boolean function for that or couldn't come in a simple way.

I have tried using REgrex but it turns the function really complex.

I am using the codemirror Api codemirror.net/doc/manual.html#api

Joe
  • 419
  • 2
  • 11
  • 21
  • That depends on the use-case, if you only want to know if the parenthesis match, you would loop over each char in the string and increase/decrease the parenthesis counter base on the occurrence of `(` or `)` , and ensure that it is never negative, and `0` at the end of the string. – t.niese Apr 27 '20 at 10:43
  • If the linked duplicate does not answer the question, you need to explain why. – t.niese Apr 27 '20 at 10:45
  • You need to check for balanced parentheses/brackets.However that will just tell you if there is an extra closing/opening one not *where*. You need more complex analysis to find out which one is missing. And yes, this shouldn't be done with regex, as it's infinitely recursive and rather complex when you have repeatedly nested regex expressions (which some regex engines have native support for but it's still annoying). – VLAZ Apr 27 '20 at 10:46
  • 1
    If you have a more complex scenario you would write a tokenizer and a lexer. – t.niese Apr 27 '20 at 10:46
  • i think this will help you - https://stackoverflow.com/questions/52969755/how-to-check-the-sequence-of-opening-and-closing-brackets-in-string – Mukund Apr 27 '20 at 10:52
  • @t.niese I am using the codemirror api. I think I could use the method eatWhile with a Regex expression. codemirror.net/doc/manual.html#api – Joe Apr 27 '20 at 13:59

0 Answers0