实际开发中我们常常使用str.replace()来做替换。但似乎很少用到它的模式匹配功能。

看看下面妙用模式匹配的例子
var re = /(w+)s(w+)/;
var str = “John Smith”;var newstr = str.replace(re, “$2, $1”);
// Smith, John
console.log(newstr);
const caller = function(n) {
return n.replace(/(d)(?=(d{3})+$)/g, “$&,”)
}
const b = caller( 123234238329 )
// 123,234,238,329
console.log(b)
注意正则表达式?=的匹配方式。完全匹配时之匹配”d”部分
参考链接:
String.prototype.replace() – JavaScript | MDN
w3c
© 版权声明
文章版权归作者所有,未经允许请勿转载。如内容涉嫌侵权,请在本页底部进入<联系我们>进行举报投诉!
THE END
















暂无评论内容