我一直在尝试不同的函数来替换DIVblahhhh@blahhh.blahhh
中的任何函数,但没有成功。contentEditable
正则表达式存在问题,或者[String].replaceAll
不是 Chrome 中现有的原型,所以我需要使用replaceAll
我在网上找到的任何原型。
用自定义模式替换字符串中的所有电子邮件的跨浏览器(Chrome/WebKit/Moz)算法应该是什么?
我一直在尝试不同的函数来替换DIVblahhhh@blahhh.blahhh
中的任何函数,但没有成功。contentEditable
正则表达式存在问题,或者[String].replaceAll
不是 Chrome 中现有的原型,所以我需要使用replaceAll
我在网上找到的任何原型。
用自定义模式替换字符串中的所有电子邮件的跨浏览器(Chrome/WebKit/Moz)算法应该是什么?
replaceAll
确实不是标准函数,但正则表达式应该可以工作:
像这样简单的东西:
[A-Z0-9._%+-]+@[A-Z0-9.-]+.[AZ]{2,}
已经可以很好地工作了:
var s = "sample@mail.com is a sample email address with an @, as is some.mail@some.government";
s.replace(/([A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,})/ig, '<tag>$1</tag>');
// "<tag>sample@mail.com</tag> is a sample email address with an @, as is <tag>some.mail@some.government</tag>";
# Match:
# ( --> Start group
# [A-Z0-9._%+-]+ --> one or more characters within the specified range,
# @ --> Followed by an `@`,
# [A-Z0-9.-]+ --> Followed by some more characters,
# \. --> Followed by an dot,
# [A-Z]{2,} --> followed by 2 or more letters,
# ) --> End group.
# ig --> (i)gnore case, (g)lobal.
# In the replacement:
# $1 --> Content of the first pair of `()`