Regular Expressions 101

Community Patterns

Mask an email regex

4

Regular Expression
PCRE (PHP <7.3)

/
(?<=.).(?=[^@]*?.@)|(?:(?<=@.)|(?!^)\G(?=[^@]*$))(.)(?=.*\.)|(?:(?<=\.)(?=[^.]+$)|(?!^)\G(?=[^@.]*$))[^.](?!$)
/
gm

Description

Replaces second to last but one characters in email with asterisks (@ and last . are protected): john.travolta@gmail.com > j***********a@g****.**m

Submitted by Wiktor Stribiżew - 8 years ago