I’m working on a regex to extract a raw string of JS-like functions, where I’d like to extract the method/function name and the parameters/arguments to the method/function:
.replace("hello", "world").trim().replace("chicken", "fried").upper()
Regex: \.(\w+)\((.*?)\)
This works for the above string… but if there are parenthesis inside of the surrounding () then it does not match fully.
I’ve been struggling with positive and negative lookahead/behinds and wonder if this is even possible for something like this?
Here is an example below where it does not work:
.replace(/(test)/g, "").trim().replace("", "").upper()
>Solution :
As Wiktor said, a dedicated parser is always the best option for this kind of processing.
However, if you still want to go with the regex route, you could simply add a positive lookahead that look for either a dot, a semi-colon or the end of the string.
\.(\w+)\((.*?)\)(?=\.|$|;)
You can test more cases here: https://regex101.com/r/QJ1h59/1
This is not perfect, though. If the regex inside the call is using a dot right after the parenthesis, the regex will not match the correct string. There will always be cases like this, you would be better off using a dedicated parser.