【发布时间】:2020-01-03 17:59:10
【问题描述】:
结合以前的几个答案,我试图组合一个正则表达式,它可以让我替换大括号内所有出现的任何东西
我已经走了这么远,但它似乎不起作用
var str = "The {type} went to the {place}";
var mapObj = {
type: 'Man',
place: "Shop"
};
var re = new RegExp(/(?<=\{)Object.keys(mapObj).join("|")(?=\})/, "gim");
str = str.replace(re, function(matched){
return mapObj[matched.toLowerCase()];
});
console.log(str);
我在上一个答案中添加了 (?
上一个答案 - Replace multiple strings with multiple other strings
【问题讨论】:
-
我会尝试简单地
/\{([^}]+)\}/匹配任何被花括号包围的字符串。
标签: javascript regex