gpt4 book ai didi

javascript - 正则表达式:查找函数第一个参数使用的属性

转载 作者:行者123 更新时间:2023-12-02 22:49:27 24 4
gpt4 key购买 nike

在我的代码中,我需要正则表达式来查找所有这些函数字符串情况下第一个参数的动态键:

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);

请帮助我,因为我对正则表达式很糟糕=(

最佳答案

对于这个例子,它会起作用

"functionString".match(/(return |=> )[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]

关于javascript - 正则表达式:查找函数第一个参数使用的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58242384/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com