简体   繁体   中英

Using regular expressions in JavaScript for checking pattern reuse (e.g. the same sequence of characters declared twice in string)

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 DB 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.

This regexp will test if there is a name, followed by an arbitrary amount of garbage, ending in the same name:

var re = /^(\w+)\b.+\b\1$/;
re.test( "GARNSEY B R & D B GARNSEY" ); // true
re.test( "GARNSEY B R & D GARNSEY" );   // true
re.test( "GARNSEY B R & D GURNSEY" );   // false
re.test( "GARNSEY B R & D ZGARNSEY" );  // false

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");
})()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM