2

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!

4
  • I doubt this is possible - and even if then it's probably very, very complex. You could use a (partial) JavaScript parser, but that also be very large. Could you explain what your overall problem is? Why do you need to do this? Commented Apr 20, 2011 at 12:26
  • I want to use eval() on it. But I want to check its valid js function first because I dont want to run variables or invalid strings. Commented Apr 20, 2011 at 12:28
  • even if the syntax is correct, if i call a non-existent method it will still cause an error. For example eval('nameOfFuncThatDoesNotExist()');. Eval is a bad thing, even-more-so in not controlled environments where the input is not pre-sanitized.. Commented Apr 20, 2011 at 12:30
  • Can you expand even more on this? Where are the strings coming from? Why do you need to eval them? Commented Apr 20, 2011 at 12:39

2 Answers 2

3

Answering the implied question from your comment:

I want to use eval() on it. But I want to check its valid js function first because I dont want to run variables or invalid strings.

If this is one of the, oh, three (or fewer) valid places where using eval isn't a bad idea, probably better to go ahead and eval it and handle the resulting Error via a try/catch block — 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.:

try {
    eval(theString);
}
catch (e) {
    // Something went wrong, report it
}

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 (your hello("hello", "world")), just have them give you a function you call without arguments, and they would give you a wrapper e.g.:

callTheFunction(function() {
    hello("hello", "world");
});
Sign up to request clarification or add additional context in comments.

Comments

2

You can define the folloding regexp for a parameter, meaning string or integer or float or variable

string literal : "[^"]"
int : -?\d+
float : -?\d*\.\d+
variable / function name : [\w\d_]+

parameter : ("[^"]"|-?\d+|-?\d*\.\d+|[\w\d_]+)

Then your function is the same as a variable, followed by parenthesis a few parmeters, then closing parenthesis.

rough : function \( (|parameter(,parameter)*\)
adding potential spaces : function\s*\(\s*(|parameter\s$(,\s*parameter\s*)*\)
replacing blocks : [\w\d_]+\s*\(\s*(|("[^"]"|-?\d+|-?\d*\.\d+|[\w\d_]+)\s$(,\s*("[^"]"|-?\d+|-?\d*\.\d+|[\w\d_]+)\s*)*\)

Note that you will want to match multiline and case insensitive. I don't ensure it's error-proof, but at least you can follow the kind of thinking you have to deal with to fine tune it.

EDIT : I thought it was fairly complex (I probably issed some cases), but after your comment, if it's just to run eval, it's pointless... note that my checks only assume that you use plain variables, no objects (otherwise, deal with the dot which can't be in first position)

1 Comment

I ask title for overcomplex perl-like make-your-eyes-bleed solution :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.