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?