gpt4 book ai didi

javascript - 为什么 $1...$9 属性 (RegExp) 不适用于 IE10?

转载 作者:行者123 更新时间:2023-11-29 17:19:11 24 4
gpt4 key购买 nike

我有一个用于 IE 的旧脚本,但我不知道为什么它不能仅用于 IE10,有人对此有一些线索吗?

String.Format = function (a) {
var b = Array.prototype.slice.call(arguments, 1);
return a.replace(/{(\d+)}/g, function () { return b[RegExp.$1] });
};

最佳答案

根据 MDN , RegExp.$n 属性已弃用。

试试这个:

return a.replace(/{(\d+)}/g, function (match) { 
// match will include the {} so we strip all non-digits
return b[match.replace(/\D/g, '')];
});

或者使用第一个括号匹配来避免额外的 replace 调用:

return a.replace(/{(\d+)}/g, function (match, p1) { 
return b[p1];
});

Source

Working example

关于javascript - 为什么 $1...$9 属性 (RegExp) 不适用于 IE10?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14127011/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com