工作,我有這樣的替換字符串和它周圍的支架沒有與正則表達式
var str = '[a] is a string with many [a] but only one [b]';
一鍵多次出現的字符串,現在我有一個與他們在海峽值的鍵的對象;
var obj = {a:'the a',b:'the b'};
我試着像這樣與他們的價值觀取代那些鍵
let output = str;
for (const key in obj) {
output = output.replace(new RegExp('[[' + key + ']]', 'g'), obj[key]);
}
其輸出
[the a is a string with many [the a but only one [the b
任何機構可以告訴我缺少的是什麼?
編輯
我怎麼能代替@[a](a)
與the a
和@[b](b)
與the b
?即
var str = '@[a](a) is a string with many @[a](a) but only one @[b](b)';
你能回答編輯的問題嗎? – ahmadalibaloch
當然,讓我看看。順便說一句,你不需要取消我的答案,要求進一步增強。我總是盡最大努力回答所有問題。 – anubhava
好的,我在等你的答案。 – ahmadalibaloch