I am trying to extract function notation from a string in javascript (so I can't use lookbehind), here are some examples:
- f(2) should match f(
- f(10)g(8) should match f( and g(
- f(2+g(3)) should match f( and g(
- f(2+g(sqrt(10))) should match f( and g(
- f(g(2)) should match f( and g(
Right now I am using
/\b[a-z]\([^x]/g
because I don't want to match when it is a string of letters (such as sqrt) only when there is a single letter then a parentheses. The problem I am having is with the last one in the list (nested functions). ( is not part of the \b catches so it doesn't match.
My current plan is to add a space after every ( using something like
input = input.replace(/\([^\s]/g, '( ');
Which splits the nested function so that \b comes into play [becomes f( g( 3))] but before I started messing with the input string, I thought I would ask here if there was a better way to do it. Obviously regex is not something I am super strong with but I am trying to learn so an explanation with the answer would be appreciated (though I will take any pointers that I can google myself too! I am not entirely sure of what to search for here.)
/\b[a-z]\(/gBased on the rules does this works ?