[英]Rewrite this regex without using lookbehind - invalid regex with JS
Only minimal experience with Regex, I am trying to implement some email masking in node.js, all was well running it locally but once pushed up to the server I am getting invalid Regex errors.只有很少的正则表达式经验,我正在尝试在 node.js 中实现一些电子邮件屏蔽,一切都在本地运行良好,但是一旦推送到服务器,我就会收到无效的正则表达式错误。
The Regex code example can be found here可以在此处找到正则表达式代码示例
https://regexr.com/42uid https://regexr.com/42uid
var email = 'foo@bar.com'
const regex = /(.)[^@\n](?=[^@\n]*[^@\n]@)|(?:(@.)|(?!^)\G(?=[^@]*$)).(?!$)/g;
const maskedEmail = email.replace(regex, '*');
maskedEmail should return maskedEmail 应该返回
f*o@b*r.com
I have narrowed the issue down to being the 'lookbehind/lookahead' which as I understand it is not available in JS.我已将问题缩小为“后视/前瞻”,据我所知,它在 JS 中不可用。 However I am not aware how best to re-write it.但是我不知道如何最好地重写它。
You can capture it in multiple groups and then retrieve that data in the replace with $1
, $2
, etc.您可以在多个组中捕获它,然后在替换为$1
、 $2
等中检索该数据。
By using this regex: ^(.).*(.@.).*(.\\.[^\\.]+)$
通过使用这个正则表达式: ^(.).*(.@.).*(.\\.[^\\.]+)$
and using the following replace string: $1*$2*$3
并使用以下替换字符串: $1*$2*$3
it will result in: f*o@b*r.com
它将导致: f*o@b*r.com
Link to my Fiddle: https://regexr.com/42um8链接到我的小提琴: https : //regexr.com/42um8
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.