Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

186
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda