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

253
Visualizações
Validate text with javascript RegEX

I'm trying to validate text with javascript but can find out why it's not working.

I have been using : https://regex101.com/ for testing where it works but in my script it fails

    var check = "test"
    var pattern = new RegExp('^(?!\.)[a-zA-Z0-9._-]+$(?<!\.)','gmi');
    if (!pattern.test(check)) validate_check = false;else validate_check = true;

What i'm looking for is first and last char not a dot, and string may contain [a-zA-Z0-9._-]

But the above check always fails even on the word : test

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

0

+$(?<!\.) is invalid in your RegEx

  1. $ will match the end of the text or line (with the m flag)
  2. Negative lookbehind → (?<!Y)X will match X, but only if Y is not before it
about 4 years ago · Juan Pablo Isaza Relatório

0

A few notes about the pattern:

  • You are using the RegExp constructor, where you have to double escape the backslash. In this case with a single backslash, the pattern is ^(?!.)[a-zA-Z0-9._-]+$(?<!.) and the first negative lookahead will make the pattern fail if there is a character other than a newline to the right, that is why it does not match test
  • If you use the /i flag for a case insensitive match, you can shorten [A-Za-z] to just one of the ranges like [a-z] or use \w to match a word character like in your character class
  • This part (?<!\.) using a negative lookbehind is not invalid in your pattern, but is is not always supported

For your requirements, you don't have to use lookarounds. If you also want to allow a single char, you can use:

^[\w-]+(?:[\w.-]*[\w-])?$
  • ^ Start of string
  • [\w-]+ Match 1+ occurrences of a word character or -
  • (?: Non capture group
    • [\w.-]*[\w-] Match optional word chars, a dot or hyphen
  • )? Close non capture group and make it optional
  • $ End of string

Regex demo

const regex = /^[\w-]+(?:[\w.-]*[\w-])?$/;
["test", "abc....abc", "a", ".test", "test."]
.forEach((s) =>
  console.log(`${s} --> ${regex.test(s)}`)
);

about 4 years ago · Juan Pablo Isaza Relatório

0

What about more simpler RegEx?

var checks = ["test", "1-t.e_s.t0", ".test", "test.", ".test."];
checks.forEach(check => {
  var pattern = new RegExp('^[^.][a-zA-Z0-9\._-]+[^.]$','gmi');
  console.log(check, pattern.test(check))
});

Your code should look like this:

var check = "test";
var pattern = new RegExp('^[^.][a-zA-Z0-9\._-]+[^.]$','gmi');
var validate_check = pattern.test(check);
console.log(validate_check);

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