1

我一直在尝试不同的函数来替换DIVblahhhh@blahhh.blahhh中的任何函数,但没有成功。contentEditable正则表达式存在问题,或者[String].replaceAll不是 Chrome 中现有的原型,所以我需要使用replaceAll我在网上找到的任何原型。

用自定义模式替换字符串中的所有电子邮件的跨浏览器(Chrome/WebKit/Moz)算法应该是什么?

4

1 回答 1

4

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 `()`
于 2013-01-07T07:55:01.617 回答