This is a bad idea. There are lots of things that could go wrong. You could look into sandboxing.
But, as long as you aren't relying on this for security, you might find this useful:
var x = function (a, b, c) { if(a) {console.log(a)}};
var y = function (a, b, c) { if(b) {console.log(a)}};
// You can get the text of a function. Notice it's been formatted.
console.log(x.toString());
>>> "function (a, b, c) { if (a) { console.log(a) } }"
var matcher = /if ?\(.?a.?\)/g;
x.toString().match(matcher);
>>> ["if (a)"]
y.toString().match(matcher);
>>> null
Things to be careful of, off the top of my head:
- Different browsers may format the code differently.
- Variables can be aliased by assigning them to a different name.
- Variables can be accessed by index-access and strings.
- This is a naïve regular expression and will obviously match
if (nota).
- Your javascript will be visible, so anyone who wants to get round this will find a way.
var b = a; if (b){...}? (indirect usage ofa)