var val = 'HeLLo WOrld'; var ex = "replace(/s*/g,'-')"; var test = new Function('return val.' + ex + ';'); alert( test() );