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

98
Visualizações
combine regex to match subgroups in different order

I have a string with may have one of the 2 below structure

(\w+){1}([!][a-zA-Z0-9>,]+)*([#]\d+)?

(\w+){1}([#]\d+)?([!][a-zA-Z0-9>,]+)*

some examples would be

"Abc1!deF2>Ghi3,4jlmNO!pQr5st#1400"
"Abc1#1400!deF2>Ghi3,4jlmNO!pQr5st"

The goal is to match as below

["Abc1", "!deF2>Ghi3,4jlmNO", "!pQr5st", "#1400"]
["Abc1", "#1400", "!deF2>Ghi3,4jlmNO", "!pQr5st"]

I can manage to get the result with 3 regex, but not with 1

const a = str.match(/\w+/)
const b = str.match(/([!][a-zA-Z0-9>,]+)/g)
const c = str.match(/[#]\d+/)

How can I get the expected result with a single regex ?

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

0

Maybe use | to separate possible matches:

const regExp = /\w+|![a-zA-Z0-9>,]+|#\d+/g;

console.log("Abc1!deF2>Ghi3,4jlmNO!pQr5st#1400".match(regExp));
console.log("Abc1#1400!deF2>Ghi3,4jlmNO!pQr5st".match(regExp));

Also note that (\w+){1} is equivalent to \w+, and [!] and [#] are the same as ! and # respectively.

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