gpt4 book ai didi

javascript - jQuery 替换为变量不起作用

转载 作者:行者123 更新时间:2023-12-02 14:23:35 25 4
gpt4 key购买 nike

我正在尝试用 data.replace 替换字符串,如果使用硬代码或静态值,它工作正常。但现在我想用循环替换多个值,但它不起作用。

我的代码:

for(var i = 0; i<words.length; i++){
var r = words[i];
data = data.replace(/\[(\[qid:{r})\]]/g, words[i]);
}

单词包含:

Array [ "hid_1", "hid_2", "hid_6", "hid_7" ]

我的数据是:

Site: [[qid:hid_1]]<br>

Block: [[qid:hid_2]]<br>

Nimewo kay la: [[qid:hid_6]]<br>

Latitude: [[qid:hid_7]]

它是 HTML 内容。

我在这里只需要变量:

for(var i = 0; i<words.length; i++){

var r = words[i];
data = data.replace(/\[(\[qid:hid_1)\]]/g, 'test');
//data.replace(/\[(\[qid:{r})\]]/g, 'test');

}

最佳答案

var words = [ "hid_1", "hid_2", "hid_6", "hid_7" ];

var data = "Site: [[qid:hid_1]]<br>\
Block: [[qid:hid_2]]<br>\
Nimewo kay la: [[qid:hid_6]]<br>\
Latitude: [[qid:hid_7]]";

for(var i = 0; i<words.length; i++){
var r = words[i];
var reg = new RegExp('\\[\\[qid:' + r +'\\]\\]');
data = data.replace(reg, r);
}

关于javascript - jQuery 替换为变量不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38483967/

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