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 `()`