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

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