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

224
Vistas
regular expression with 3 conditions with javascript and typescript

I want to validate a certain element in xml by using regular expression.

<ConfigOption>value</ConfigOption>

Requirements for ConfigOption:

  1. characters, numbers, underscore and space are allowed.
  2. CONFIG-XX should be allowed. XX is numbers
  3. CONFIGNAME?config=XX format should be allowed. xx is numbers.

Here is what I have.

(^[a-zA-Z0-9 _]*$)|(?:\??config=\d)|(^CONFIG\-\d$)

for example: 'hello' should be ok. 'hello?config=20' should be ok 'CONFIG-20' should be ok. 'hello-' should fail 'hello*' should fail

can someone fix my regular expression? when I test 'hello=' or 'hello*' 'https://regex101.com/', it still matches.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Here is a solution I think matches your requirements. Let me know if it needs refining.

The real difference is in adding the lookahead (?=\W).

Note the third condition is redundant and unneeded.

const regex = /(?=\W)(^\b[a-zA-Z0-9 _]*)|(^[a-zA-Z0-9 _]*(?:\??[Cc][Oo][Nn][Ff][Ii][Gg][=,-]\d{2}))/;
const tests = ['*HELLO', 'hello', 'hello=', 'hello*', 'hello?CONFIG-20', 'hello?CONFIG=20', 'config=20'];

for (const test of tests) {
  console.log(regex.test(test));
}

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