I want to check if a string is a valid javascript function call.
Could someone help me to write a regex that match to all these:
hello("hello", "world")
greet(1, "mr", "peterson");
run()
But not these:
walk()); // invalid syntax
greet(1?23\) // invalid js characters
say("hi" -) // invalid js characters
Thanks!
Answering the implied question from your comment:
If this is one of the, oh, three (or fewer) valid places where using
evalisn’t a bad idea, probably better to go ahead andevalit and handle the resultingErrorvia atry/catchblock —EvalError,SyntaxError, etc. See the spec for details of the various errors — if you care what the error is; mostly I’d think you just want to know whether there was a problem or not, e.g.:But: You can (and should) almost always avoid using
eval. For instance, if you want to allow someone to give you something to call that may require arguments (yourhello("hello", "world")), just have them give you a function you call without arguments, and they would give you a wrapper e.g.: