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

168
Visualizações
Regex not working on email domain selection properly

var str = "sdhdhh@gmail.com"; // true but coming false
var str1 = "sdhdhh@gmail.co.uk"; 
var str2 = "sdhdhh@gmail.org";
var str3 = "sdhdhh@gmail.org.uk";
var patt = new RegExp("[a-z0-9._%+-]+@[a-z0-9.-]+?[\.com]?[\.org]?[\.co.uk]?[\.org.uk]$");
console.log( str + " is " + patt.test(str));
console.log( str1 + " is " + patt.test(str1));
console.log( str2 + " is " + patt.test(str2));
console.log( str3 + " is " + patt.test(str3));

Can anyone tell me what is the mistake, my .com example is not working properly

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You need

  • A grouping construct instead of character classes
  • A regex literal notation so that you do not have to double escape special chars
  • The ^ anchor at the start of the pattern since you need both ^ and $ to make the pattern match the entire string.

So you need to use

var patt = /^[a-z0-9._%+-]+@[a-z0-9.-]+?(?:\.com|\.org|\.co\.uk|\.org\.uk)$/;

See the regex demo.

If you need to make it case insensitive, add i flag,

var patt = /^[a-z0-9._%+-]+@[a-z0-9.-]+?(?:\.com|\.org|\.co\.uk|\.org\.uk)$/i;
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