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 compiler 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.
This is totally impossible to do with a regex.
Consider
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.