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

150
Visualizações
Javascript regex for matching valid characters without repetition of a particular character
let rx = /(\/MxML\/[a-z0-9\[\]@/]*)/gi;
let s =
    'If (/MxML/trades[1]/value== 1 then /MxML/trades[type=2]/value must be /MxML/stream/pre/reference@href';

let m;
let res = [];

while ((m = rx.exec(s))) {
    res.push(m[1]);
}
console.log(res);
  1. Pattern should start with /MxML/
  2. Keep including acceptable characters [a-z0-9[]@/]
  3. A single = is ok. But == or === are not ok.

Desired output

[
    '/MxML/trades[1]/value',
    '/MxML/trades[type=2]/value',
    '/MxML/stream/pre/reference@href',
];

My current (incorrect) output

[
    '/MxML/trades[1]/value',
    '/MxML/trades[type',
    '/MxML/stream/pre/reference@href',
];

I can think of a lookaround, but can't figure out how to use it here. (?<!\=)\=(?!\=)

I've been going through several other examples on Stackoverflow, but they are about avoiding all character repetition and not about avoiding a particular character(=) repetition in conjunction with other valid characters.

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

0

Here is a regex which should be compatible in all browsers:

/(\/MxML\/(?:[a-z0-9\[\]@/]|=(?!=))*)/gi
  • /(\/MxML\/ - your regex
  • (?: - start non-capture group; we need this to implement an "or" operator and not mess up your use of m[1]
    • [a-z0-9\[\]@/] - your regex of whitelisted chars; notice the asterisk was removed
    • | - or
    • =(?!=) - allow for an equal sign not followed by an equal sign
  • )* - close non-capturing group and capture between zero and infinity instances of what's inside
  • )/gi - your regex

let rx = /(\/MxML\/(?:[a-z0-9\[\]@/]|=(?!=))*)/gi;
let s =
    'If (/MxML/trades[1]/value== 1 then /MxML/trades[type=2]/value must be /MxML/stream/pre/reference@href';

let m;
let res = [];

while ((m = rx.exec(s))) {
    res.push(m[1]);
}
console.log(res);

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