这段正则为什么报语法错误,我在浏览器里运行可以但是小程序wxs里报错?-灵析社区

文文问问

这段正则为什么报语法错误,我在浏览器里运行可以但是小程序wxs里报错 ![image.png](https://wmlx-new-image.oss-cn-shanghai.aliyuncs.com/images/20241109/76595fdcaa962634f59ade91eefb75cf.png) ![image.png](https://wmlx-new-image.oss-cn-shanghai.aliyuncs.com/images/20241109/4c2ce9aeb98c02afccea790d0ea55669.png) // utils.wxs var formatPhone = function (val) { if (val) { const matches = /^(\d{3})(\d{4})(\d{4})$/.exec(val) if (matches) { return matches[1] + ' ' + matches[2] + ' ' + matches[3] } } return val } 这段正则为什么报语法错误

阅读量:15

点赞量:0

问AI
报错已经提示了"/"这个符号出的问题, 因为wsx不支持使用正则表达式字面量写法 wxs正则需要调方法: var telEncrypt = function(str){ // wxs页面调用 var talReg = getRegExp('^[1][3,4,5,7,8,9][0-9]{9}$') var length = str.length if (talReg.test(str)) { return str.substring(0, 3) + "****" + str.substring(length - 4, length) } else { return str } } var a = getRegExp("x", "img"); console.log("x" === a.source); console.log(true === a.global); console.log(true === a.ignoreCase); console.log(true === a.multiline); 官方文档 «"https://developers.weixin.qq.com/miniprogram/dev/reference/wx..." (https://link.segmentfault.com/?enc=5tUyqpLdT%2FGDaFIcb6%2F5Cw%3D%3D.zrsoAK%2BLEIG%2BNGlJogDaLG3eod01GFdKmP6M43yZfI6swMKu6k8yk6Nh5im3zDUGSm5fp4UeoZjDUeJkRxkzHefI9pptoZLpkx2gSyw889dQde1SDP9Mf5A5utSe9EjU)»