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

167
Visualizações
Regex Behaves Differently on HTML Pattern than on an Express Backend

I have the following regex pattern on an HTML input field, which is supposed to hold an email address:

<input type="text" pattern="^\w+([.-]?\w+)*@\w+([.-]?\w+)*(\.\w{2,4})+$" /><br>

I furthermore have the same regex on an Express (JavaScript) backend using the following:

 var re-email = new RegExp("^\w+([.-]?\w+)*@\w+([.-]?\w+)*(\.\w{2,4})+$")
   if (!re-email.test(email)) {
     validation = false
   }

Although the regex are exactly the same, a specific test input is evaluated as true on the front-end while as false on the backend.

Why is this?

Solution (found after the initial post):

Instead of using "new RegExp" (which is not working) as above, include the Regex within forward slashes as below (which works).

var re-email = /^\w+([.-]?\w+)@\w+([.-]?\w+)(.\w{2,4})+$/

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

0

Probably not the answer you are after (not vue.js specific)...

Email address input validation should usually be completed like so:

<input type="email" name="" value="" required />

Specifying the correct "type" to an input field also adjusts input keyboards on mobile devices to make inputting an email address easier.


Your regular expression is poorly written and leads to "catastrophic backtracking" as well as not actually supporting valid email addresses.

Email address validation is generally complex, see this answer and associated question: https://stackoverflow.com/a/201378/406712


You can also find the HTML email address validation equivalent regular expression in the HTML spec: https://html.spec.whatwg.org/multipage/input.html#valid-e-mail-address


Also note you failed to escape the characters in the string, the first instance being the \w which without escaping the \ will appear as simply w.

Escaped the string it more like this:

'/^\\w+([.-]?\\w+)@\\w+([.-]?\\w+)(.\\w{2,4})+$/'
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