在我的代码中,在所有这些函数字符串情况下,我都需要使用正则表达式来查找第一个参数的动态键:
x => x.id // should return 'id'
x => x.a.biggerThan(5) // should return 'a'
x=>x.b.biggerThan(5) // should return 'b'
test => test.c.biggerThan(5) // should return 'c'
(x, y) => x.d.biggerThan(5) // should return 'd'
x => { return x.e.biggerThan(5); } // should return 'e'
(x, y) => { y = test.description; return x.f.biggerThan(y); } // should return 'f'
function (x) { return x.g.biggerThan(5); } // should return 'g'
function test(x) { return x.h.biggerThan(5); } // should return 'h'
function(x) { return x.i.biggerThan(5); } // should return 'i'
function(x){ return x.j.biggerThan(5); } // should return 'j'
function (x, y) { return x.l.biggerThan(5); } // should return 'l'
-解释我为什么需要它-
这些是用toString()
方法字符串化的函数,我需要创建一个动态对象,该对象具有要在该函数中插入的键,如下所示:
// get the key used by the function
const key = getkeyInFunc(func.toString());
// construct a dynamic object using that key
const builder = {
[key]: {
biggerThan: // biggerThan function
// others methods
}
};
// call the function with the dynamic key as parameter
const result = func(builder);
请帮助我,因为我对regex =(
对于此示例,它将起作用
“ functionString” .match(/(返回| =>)[a-zA-Z_0-9] + ?.([a-zA-Z_0-9] +)/)[2]
例:
"(x, y) => { y = test.description; return x.f.biggerTha_n(y); }".match(/(return |=> )[a-zA-Z_0-9]+?\.([a-zA-Z_0-9]+)/)[2]
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句