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

194
Visualizações
Capturing groups with regular expression does not work

I want to simplify a regular expression, to remove the repetitive parts I use group capture and reference, But in the following case this doesn't work. Here the code:

let regex= /([\w\-\d]+)(\/)(\1)/g

let string1= "user-name/alias"
let string2= "user234/hello-word"

let result1= regex.test(string1)
let result2= regex.test(string2)

console.log(result1,result2)

The code above will return false, it is interesting that in this case the reference to the group does not work. The code that works would be the following:

let regex= /([\w\-\d]+)(\/)([\w\-\d]+)/g

let string1= "user-name/alias"
let string2= "user-name/alias"

let result1= regex.test(string1)
let result2= regex.test(string2)

console.log(result1)
console.log(result2)

In the latter case, the value of the two variables is the same string. Why when doing the test in the last variable I get false, should it be true?

Why does the reference to the group not work? What do I do to make it work?

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