正则从字符串中提取邮箱会把ftp地址误认为邮箱:
var string = "ftp://[email protected]/%2Fetc/motd...多字符串[email protected]"; var regex2 = /[dw]+@[a-zA-ZA-z0-9]+.[a-z]+/g; console.log(string.match(regex2)) //结果:["[email protected]", "[email protected]"]
可以使用零宽度负回顾后发断言,(?<!exp),断言此位置的前面不能匹配表达式 exp。
(?<!exp)
exp
比如你的需求,后面邮箱正则该怎么写怎么写,前面加上 (?<!ftp://) 即可。
(?<!ftp://)
这个特性是 ES2018 后加入的。
2.1m questions
2.1m answers
62 comments
56.6k users