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

173
Visualizações
matching regex pattern in javascript using RegExp

I have a string from which I want to check if it only contains characters that are allowed. I want only characters of

  • alphanumeric (lower & upper) : A-Z, a-z

  • numeric (0-9)

  • special characters: _&!.-

  • space

example of valid strings:

  • title1 - header
  • title2&&& header_!.
  • 09
  • a912
  • ...dd

example of invalid strings

  • a$$$ ()
  • a12345^&6**

if at least one of characters is not in the special characters so it not valid string. I would like to know how do test it in javascript. the regex patterns come from server so it string. I convert the regex pattern string by using RegExp but all of them show false, even some of them are valid. the three first lines should be valid (true)

 const pattern = "^[a-z0-9_&!.-\\s]+$";

                const strings = [
                  `title1 - header`,
                  `title2&&& header_!.-`,
                  `09`,
                  `a912 dd`,
                  `a$$$ ()`,
                  `a12345^&6**`
                ]
                const regex = new RegExp(pattern);

                strings.forEach(str => {

                  console.log(str, ' ---> ', regex.test(str));
                });

about 4 years ago · Juan Pablo Isaza
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