Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

194
Views
What will be the best regex Expression for censoring email?

Hello I am stuck on a problem for censoring email in a specific format, but I am not getting how to do that, please help me!

Email : exampleEmail@example.com

Required : e***********@e******.com

Help me getting this in javascript, Current code I am using to censor :

const email = exampleEmail@example.com;
const regex = /(?<!^).(?!$)/g;
const censoredEmail = email.replace(regex, '*');

Output: e**********************m

Please help me getting e***********@e******.com

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can use

const email = 'exampleEmail@example.com';
const regex = /(^.|@[^@](?=[^@]*$)|\.[^.]+$)|./g;
const censoredEmail = email.replace(regex, (x, y) => y || '*');
console.log(censoredEmail );
// => e***********@e******.com

Details:

  • ( - start of Group 1:
    • ^.| - start of string and any one char, or
    • @[^@](?=[^@]*$)| - a @ and any one char other than @ that are followed with any chars other than @ till end of string, or
    • \.[^.]+$ - a . and then any one or more chars other than . till end of string
  • ) - end of group
  • | - or
  • . - any one char.

The (x, y) => y || '*' replacement means the matches are replaced with Group 1 value if it matched (participated in the match) or with *.

about 4 years ago · Juan Pablo Isaza Report

0

If there should be a single @ present in the string, you can capture all the parts of the string and do the replacement on the specific groups.

  • ^ Start of string
  • ([^\s@]) Capture the first char other than a whitespace char or @ that should be unmodified
  • ([^\s@]*) Capture optional repetitions of the same
  • @ Match literally
  • ([^\s@]) Capture the first char other than a whitespace char or @ after it that should be unmodified
  • ([^\s@]*) Capture optional repetitions of the same
  • (\.[^\s.@]+) Capture a dot and 1+ other chars than a dot, @ or whitespace char that should be unmodified
  • $ End of string

Regex demo

In the replacement use all 5 capture groups, where you replace group 2 and 4 with *.

const regex = /^([^\s@])([^\s@]*)@([^\s@])([^\s@]*)(\.[^\s.@]+)$/;
[
  "exampleEmail@example.com",
  "test"
].forEach(email =>
  console.log(
    email.replace(regex, (_, g1, g2, g3, g4, g5) =>
      `${g1}${"*".repeat(g2.length)}@${g3}${"*".repeat(g4.length)}${g5}`)
  )
);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!