Javascript 使用正则表达式将字符串替换为具有数组元素的数组索引。滑动分页
我需要一个字符串助手用变量替换方括号内的所有内容。使用javascriptJavascript 使用正则表达式将字符串替换为具有数组元素的数组索引。滑动分页,javascript,jquery,regex,Javascript,Jquery,Regex,我需要一个字符串助手用变量替换方括号内的所有内容。使用javascript "Hello, [0]".modify(["ABC"]) "Heelo, [0], This is [1]".modify(["ABC", "XYZ"]) "Heelo, [0], This is [1], Your email address is [2]".modify(["ABC", "XYZ", "abcdef@example.com"]) 因此,基本上,modify()将获取数组并用适当的索引替换字符串
"Hello, [0]".modify(["ABC"])
"Heelo, [0], This is [1]".modify(["ABC", "XYZ"])
"Heelo, [0], This is [1], Your email address is [2]".modify(["ABC", "XYZ", "abcdef@example.com"])
因此,基本上,modify()将获取数组并用适当的索引替换字符串
任何建议都会有帮助。String.prototype.modify=function(){
String.prototype.modify = function(arr) {
return this.replace(/\[(\d+)\]/g, function(c, m) {
return arr[m] === undefined ? c : arr[m];
});
};
"Heelo, [0], This is [1]".modify(["ABC", "XYZ"]);
// "Heelo, ABC, This is XYZ"
var s=参数[0];
对于(var i=0;i
所有答案都很有用
但是我使用了内置函数,来自@PedrodelSol提供的链接
"Hello {0}, This is {1}".format(["ABC", "XYZ"])
是的,我知道,用ruby怎么做。但是我们如何在javascript中实现它呢?签出这个
"Hello {0}, This is {1}".format(["ABC", "XYZ"])