Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

193
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!