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

223
Visualizações
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 Respostas
Responde à pergunta

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 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