JSFiddle - React, Tailwind, and code Playground

by Aleksey Pshenichnov

JavaScript

//Parse function string and convert it to real javascript function...
if (typeof String.prototype.parseFunction != 'function') {
    String.prototype.parseFunction = function () {
		var funcReg = /function *\(([^()]*)\)[ \n\t]*{(.*)}/gmi;
		var match = funcReg.exec(this.replace(/\n/g, ' '));
		if(match) {
            return new Function(match[1].split(','), match[2]);
		}

    	return null;
    };
}

$(document).ready(function() {
    var func = 'function (a, b) { return a + b; }'.parseFunction();
    alert(func(3,4));

    func = 'function () { alert("Hello from function initiated from string!"); }'.parseFunction();
    func();
});