JavaScript字符串数字替换:将0{#3#}1{#1#}2{#2#}3{#4#}改成0{#1#}1{#2#}2{#3#}3{#4#}
你可以使用正则表达式和replace方法来实现这个需求。
var str = "0{#3#}1{#1#}2{#2#}3{#4#}";
var result = str.replace(/\{#(\d)#\}/g, function(match, num) {
return "{#" + (parseInt(num) - 1) + "#}";
});
console.log(result);
这段代码中,我们使用了正则表达式/\{#(\d)#\}/g来匹配形如{#数字#}的字符串。然后使用replace方法,将匹配到的字符串替换成{#数字-1#}的形式。最后得到的结果就是"0{#1#}1{#2#}2{#3#}3{#4#}"。
原文地址: https://www.cveoy.top/t/topic/pO9t 著作权归作者所有。请勿转载和采集!