用消息中的值替换占位符-Javascript

用消息中的值替换占位符-Javascript,javascript,Javascript,我有一个如下所示的JavaScript消息对象,它有一些如下所示的占位符 { message: "This is a Sample message with some placeholder values like {placeHolder1}, {placeHolder2} ad {placeHolder3}" "context_types": [ "placeHolder1", &qu

我有一个如下所示的JavaScript消息对象,它有一些如下所示的占位符

{
    message: "This is a Sample message with some placeholder values like {placeHolder1}, {placeHolder2} ad {placeHolder3}"
    "context_types": [
        "placeHolder1",
        "placeHolder2",
        "placeHolder3"
    ],
    "context_values": [
        "Value1",
        "Value2",
        "Value3"
    ]
}
最后的信息应该是

This is a Sample message with some placeholder values like Value1, Value2 ad Value3

我需要对对象数组执行此操作。伙计们帮我解决这个问题,需要一个util函数来转换它。

只要占位符值没有任何正则表达式元字符,就可以在数组上循环并使用
String\replace

const obj={
message:“这是一个示例消息,具有一些占位符值,如{placeholder 1}、{placeholder 2}ad{placeholder 3}”,
“上下文类型”:[
“占位符1”,
“占位符2”,
“占位符3”
],
“上下文值”:[
“价值1”,
“价值2”,
“价值3”
]
}
设str=obj.message;
obj.context_types.forEach((占位符,idx)=>str=str.replace(新的RegExp(“{”+占位符+“}”,“g”),obj.context_值[idx]);

console.log(str)
只要占位符值没有任何正则表达式元字符,就可以在数组上循环并使用
String#replace

const obj={
message:“这是一个示例消息,具有一些占位符值,如{placeholder 1}、{placeholder 2}ad{placeholder 3}”,
“上下文类型”:[
“占位符1”,
“占位符2”,
“占位符3”
],
“上下文值”:[
“价值1”,
“价值2”,
“价值3”
]
}
设str=obj.message;
obj.context_types.forEach((占位符,idx)=>str=str.replace(新的RegExp(“{”+占位符+“}”,“g”),obj.context_值[idx]);

console.log(str)使用带有回调的正则表达式替换。A
/\{(+?)\}/g
就可以了。它查找类似匹配的子字符串
“{text here}”
,它将
{
}
中的文本分组,以将其作为回调的单独参数,它还使用非贪婪/惰性量词
+?

之后,只需使用普通的old
indexOf
来获取值的索引

function getMessage(obj) {
    return obj.message.replace(/\{(.+?)\}/g, function(match, placeholder) { // go over all placeholders in the string
        var index = obj.context_types.indexOf(placeholder);                 // get the index of the current placeholder from the context_types array
        if(index !== -1) {                                                  // if this placeholder exists
            return obj.context_values[index];                               // return the value that corresponds to it
        }
        return match;                                                       // otherwise return match which will not replace the current placeholder in the resulting string, you can use a different return value like "undefined" to signal placeholder not found
    });
}
演示:

函数getMessage(obj){ 返回obj.message.replace(/\{(+?)\}/g,函数(匹配,占位符){ var index=obj.context\u types.indexOf(占位符); 如果(索引!=-1){ 返回对象上下文_值[索引]; } 复赛; }); } 常量对象={ message:“这是一个示例消息,具有一些占位符值,如{placeholder 1}、{placeholder 2}ad{placeholder 3}”, “上下文类型”:[ “占位符1”, “占位符2”, “占位符3” ], “上下文值”:[ “价值1”, “价值2”, “价值3” ] }
console.log(getMessage(obj))使用带有回调的正则表达式替换。A
/\{(+?)\}/g
就可以了。它查找类似匹配的子字符串
“{text here}”
,它将
{
}
中的文本分组,以将其作为回调的单独参数,它还使用非贪婪/惰性量词
+?

之后,只需使用普通的old
indexOf
来获取值的索引

function getMessage(obj) {
    return obj.message.replace(/\{(.+?)\}/g, function(match, placeholder) { // go over all placeholders in the string
        var index = obj.context_types.indexOf(placeholder);                 // get the index of the current placeholder from the context_types array
        if(index !== -1) {                                                  // if this placeholder exists
            return obj.context_values[index];                               // return the value that corresponds to it
        }
        return match;                                                       // otherwise return match which will not replace the current placeholder in the resulting string, you can use a different return value like "undefined" to signal placeholder not found
    });
}
演示:

函数getMessage(obj){ 返回obj.message.replace(/\{(+?)\}/g,函数(匹配,占位符){ var index=obj.context\u types.indexOf(占位符); 如果(索引!=-1){ 返回对象上下文_值[索引]; } 复赛; }); } 常量对象={ message:“这是一个示例消息,具有一些占位符值,如{placeholder 1}、{placeholder 2}ad{placeholder 3}”, “上下文类型”:[ “占位符1”, “占位符2”, “占位符3” ], “上下文值”:[ “价值1”, “价值2”, “价值3” ] } console.log(getMessage(obj))
const数据={
message:“这是一个示例消息,具有一些占位符值,如{placeholder 1}、{placeholder 2}和{placeholder 3}”,
“上下文类型”:[
“占位符1”,
“占位符2”,
“占位符3”
],
“上下文值”:[
“价值1”,
“价值2”,
“价值3”
]
}
函数生成(数据){
const message=data.message.split(/{\w+}/g).slice(0,-1);
返回消息.reduce((string,ele,i)=>string+=ele+=data.context_值[i],“”);
}
console.log(生成(数据))
const数据={
message:“这是一个示例消息,具有一些占位符值,如{placeholder 1}、{placeholder 2}和{placeholder 3}”,
“上下文类型”:[
“占位符1”,
“占位符2”,
“占位符3”
],
“上下文值”:[
“价值1”,
“价值2”,
“价值3”
]
}
函数生成(数据){
const message=data.message.split(/{\w+}/g).slice(0,-1);
返回消息.reduce((string,ele,i)=>string+=ele+=data.context_值[i],“”);
}

console.log(生成(数据))
如果
上下文
是这样一个对象,它会更干净:
{message:“…”,上下文:{Placeholder 1:value1,Placeholder 2:value2,…}
如果
上下文
是这样一个对象,它会更干净:
{message:“…”,上下文:{Placeholder 1:value1,Placeholder 2:value2,…}