I need to make sure that some submitted code has a function named mapfn
defined, and that that function returns a result. I came up with the following regex expression:
mapfn\s+?\=\s+?function\s+?\(split\)\s+?\{.+?return\(result\).+?\}
which matches something like
mapfn = function (split) {
var i = 5+4;
for (var j = 0; j < 10; j++) {
i += j*Math.random()*10;
}
var result = i;
return(result)
}
Which is desirable but if I go to for an example closure piler with this code and get something like mapfn=function(){for(var b=9,a=0;a<10;a++)b+=a*Math.random()*10;return b};
, that regex is useless. Also, the user submits something like
function mapfn (split) {
var i = 5+4;
for (var j = 0; j < 10; j++) {
i += j*Math.random()*10;
}
var result = i;
return(result)
}
Then the regex is also useless.
I feel there's a more elegant solution for this problem than having 5 or 6 regular expressions for this job and trying to match any one of them.
I need to make sure that some submitted code has a function named mapfn
defined, and that that function returns a result. I came up with the following regex expression:
mapfn\s+?\=\s+?function\s+?\(split\)\s+?\{.+?return\(result\).+?\}
which matches something like
mapfn = function (split) {
var i = 5+4;
for (var j = 0; j < 10; j++) {
i += j*Math.random()*10;
}
var result = i;
return(result)
}
Which is desirable but if I go to for an example closure piler with this code and get something like mapfn=function(){for(var b=9,a=0;a<10;a++)b+=a*Math.random()*10;return b};
, that regex is useless. Also, the user submits something like
function mapfn (split) {
var i = 5+4;
for (var j = 0; j < 10; j++) {
i += j*Math.random()*10;
}
var result = i;
return(result)
}
Then the regex is also useless.
I feel there's a more elegant solution for this problem than having 5 or 6 regular expressions for this job and trying to match any one of them.
Share Improve this question asked Sep 13, 2011 at 0:00 João Pinto JerónimoJoão Pinto Jerónimo 9,88420 gold badges64 silver badges87 bronze badges2 Answers
Reset to default 8This is totally impossible to do with a regex.
Consider
function x() { if (1 < 0) return 7; }
All you can do is parse the function in a Javascript environment, call it, and see what it returns.
If you want to make sure that it always returns a value, you'll need to solve the Halting Problem.
SLaks is right, but you don't need to invoke the halting problem to prove it's impossible with regular expressions. With a regular expression, you can't even tell whether a JavaScript function contains return
used as a keyword even if you could match curly brackets to tell where a definition function ends.
Consider
function () {
1 /[/**/]; return 1 ///
}
vs
function () {
/[/**/]; return 1 ///
}
The first contains a return because it is equivalent to
function () {
1 / [];
return 1;
///
}
but the second does not because it is equivalent to
function () {
new RegExp("[\\/\\*\\*\\/]; return 1 ");
//
}
There can be arbitrarily many characters between the /*
and */
so you need to pletely disambiguate the first /
to tell whether it is a division operator or a regular expression to tell whether the keyword return
occurs in the function. That requires a full parse, and JavaScript does not have a regular lexical grammar.