Regex 应用程序脚本正则表达式:语法错误:无效量词
基于此,我有以下代码:Regex 应用程序脚本正则表达式:语法错误:无效量词,regex,google-apps-script,Regex,Google Apps Script,基于此,我有以下代码: function createMessage(){ data = { "SOURCE" : "+1234567890", "DESTINATION" : "+2345678901", "FIRST_NAME" : "Jane", "LAST_NAME" : "Doe", "COUPON" : "DUMMY20", "STORE" : "PLIVO", "DISCOUNT" : "20", } templ
function createMessage(){
data = {
"SOURCE" : "+1234567890",
"DESTINATION" : "+2345678901",
"FIRST_NAME" : "Jane",
"LAST_NAME" : "Doe",
"COUPON" : "DUMMY20",
"STORE" : "PLIVO",
"DISCOUNT" : "20",
}
template_data = "Hi , your coupon code for discount of % purchase at is "
Logger.log(data);
for (var key in data) {
Logger.log(key);
if (data.hasOwnProperty(key)) {
template_data = template_data.replace(new RegExp('+key+', 'gi'),data[key]); // error here
}
}
Logger.log(template_data);
return template_data;
}
当我运行createMessage
时,我得到:
SyntaxError: Invalid quantifier +. (line 57, file "Code")
我做错了什么?如何解决此问题?正则表达式中的前导“+”是导致此问题的原因。“+”是指定应匹配多少个模式(在本例中为一个或多个)的量词。因此,如果量词没有模式,就像匹配一个或多个“nothing”(无)。为什么屏幕截图中会出现错误消息?那么如何解决这个问题?我不是正则表达式expert@user61629首先,详细地向自己解释您想要使用所创建的RegExp做什么,然后将其与您认为的
newregexp('+key+','gi')
正在做的事情(以及您认为newregexp('\+'+key+'\+','gi')
正在做的事情)进行对比。感谢您在教程中发现了一个错误。我应该读:template_data=template_data.replace(newregexp(key,'gi'),data[key]);