gpt4 book ai didi

javascript - 我们可以用字符串变量检查 match() 函数吗

转载 作者:行者123 更新时间:2023-11-30 09:30:50 24 4
gpt4 key购买 nike

我有条件检查数据来自服务的匹配功能,如下所示

var a = "daadvavda";
var b = "/[a-zA-Z]/g";
var c = a.match(b);
console.log(c)
// output will be null

我该如何解决这个问题。我在下面的代码中得到了正确的输出

var c = a.match(/[a-zA-Z]/g);
console.log(c)
//output :(9) ["d", "a", "a", "d", "v", "a", "v", "d", "a"]
// this is the output i needed

我不能这样提供我的数据

match(/[a-zA-Z]/g)

因为从服务中获取这些数据

最佳答案

要使用动态字符串作为模式,你可以让它像这样工作

var a = "daadvavda";
var b = new RegExp("stringYouGetFromService", 'g');
var c = a.match(b);
console.log(c);

关于javascript - 我们可以用字符串变量检查 match() 函数吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46379775/

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