使用mulltiple替换的javascript.replace()方法
我希望遍历逗号分隔字符串中的值列表,并为每个值修改和追加字符串。例如:使用mulltiple替换的javascript.replace()方法,javascript,jquery,regex,replace,Javascript,Jquery,Regex,Replace,我希望遍历逗号分隔字符串中的值列表,并为每个值修改和追加字符串。例如: // get input text, convert mySQLtext = "Select * from filename@@" // get substitution value (what to replace) mySubstitutonToken = "@@" // get substitution values (comma separated) mySu
// get input text, convert
mySQLtext = "Select * from filename@@"
// get substitution value (what to replace)
mySubstitutonToken = "@@"
// get substitution values (comma separated)
mySubstituton = "1,2,3,4,5,6,7,8,9"
// break string into array
mySubstitutonArray = mySubstituton.split(",");
// iterate and replace
$.each(mySubstitutonArray, function(index, val) {
myVal = mySQLtext.replace(mySubstitutonToken, val);
// put on screen
$("#output_text").append(myVal + '<br>');
});
问题:
如果输入文本多次出现替换值,我将如何处理?无法将正则表达式语法与变量名相结合
例如:
mySQLtext=从文件名中选择字段@@@@
谢谢大家! 通过替换所有引用,而不是仅替换一个引用:
myVal = mySQLtext.replace(new RegExp(mySubstitutionToken,"g"), val);
通过替换所有引用,而不是仅替换一个引用:
myVal = mySQLtext.replace(new RegExp(mySubstitutionToken,"g"), val);
此处需要在regexp中使用“g”全局标志。 您可以这样做:newregexpression、'g'或/literalExpression/g 常量标记='@'; 常量替换='1,2,3,4,5,6,7'; 常量模板='从文件@'中选择字段@'; 常量输出=替换 .分裂',' .mapnum=>template.replace 新的RegExptoken,'g', 号码
console.logoutput.join'\n'此处需要在regexp中使用“g”全局标志。 您可以这样做:newregexpression、'g'或/literalExpression/g 常量标记='@'; 常量替换='1,2,3,4,5,6,7'; 常量模板='从文件@'中选择字段@'; 常量输出=替换 .分裂',' .mapnum=>template.replace 新的RegExptoken,'g', 号码 console.logoutput.join'\n'