Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

174
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda