正则-replace实战

2019-06-16  本文已影响0人  子心_
var str = "20151213";
var ary = ["零","壹","贰","叁","肆","伍","陆", "柒","捌","玖"];
str = str.replace(/\d/g, function () {
  var num = arguments[0];
  var str = ary[num];
  return str;
});
console.log(str);

replace的分组替换实例:

var str = "my name is {0}, my age is {1}, i come from {2}, i love {3}~";
var ary = ["易文", 25, "湖南", "javaScript"];
var reg = /{(\d+)}/g;
str = str.replace(reg, function (larCon, smallCon, index, input) {
  // larCon --> arguments[0] 每一次执行replace大正则捕获的内容
// smallCon --> arguments[1] 每一次执行replace小正则捕获的内容
    return arg[arguments[1]];
});
console.log(str)
上一篇下一篇

猜你喜欢

热点阅读