Notice how these strings of text have the word "GARNSEY" declared twice:
"GARNSEY B R & D B GARNSEY"
"GARNSEY B R & D GARNSEY"
Now it can be D GARNSEY (no middle initial) or D B GARNSEY (includes middle initial) but I need to know if GARNEY is mentioned because that means last name is mentioned twice, once at beginning and once at end.
According to the book JavaScript Programmer's Reference:
"You can repeat the search for that exact symbol throughout the pattern...You can do this using \1 . Using \1 refers to the result of the first grouped expression."
Ok, so I try to "save" the result of the first group \w{1,})\1 and then I try to reuse it at the end, trying to also check if there's a middle name or not:
/^(\w{1,})\1\s\w{1,}((?:\s\w{1,})?)+\s+&\s+\w{1,}\s(((?:\s\w{1,})?)+)\1$/;
Yet the JavaScript interpreter alerts "failed" with the below simple test:
(function(){
var checkChar = function(txt){
var regex = /^(\w{1,})\1\s\w{1,}((?:\s\w{1,})?)+\s+&\s+\w{1,}\s(((?:\s\w{1,})?)+)\1$/;
(regex.test(txt)) ? alert('passed') : alert('failed');
}
checkChar("GARNSEY B R & D B GARNSEY");
})()
Am I misunderstanding the purpose of \1 and is there any solution to do what I am trying to do using a regular expression, as shown above? Thanks for response.
Remove the \1 at the beginning of the regexpr. After that it will still not report pass, but that is probably some other error in you regexpr. I tried to simplify your code to do more or less the same:
(function(){
var checkChar = function(txt){
var regex = /^(\w+)(\s\w+)+\s+&\s+(\w+\s)+\1$/;
(regex.test(txt)) ? alert('passed') : alert('failed');
}
checkChar("GARNSEY B R & D B GARNSEY");
})()