在 JavaScript 中使用正则表达式来检查模式重用(例如,在字符串中声明两次的相同字符序列)
请注意这些文本字符串如何声明两次“GARNSEY”一词:
"GARNSEY B R & D B GARNSEY"
"GARNSEY B R & D GARNSEY"
现在它可以是 D GARNSEY(无中间名首字母)或 DB GARNSEY(包括中间名首字母),但我需要知道是否提到了 GARNSEY,因为这意味着提到了姓氏两次,一次在开始,一次在结束。
根据《JavaScript 程序员参考》一书:
“您可以在整个模式中重复搜索确切的符号...您可以使用 \1 来执行此操作。使用 \1 指的是第一个分组表达式的结果。”
好的,所以我尝试“保存”第一组 \w{1,})\1 的结果,然后尝试在最后重用它,并尝试检查是否有中间名:
/^(\w{1,})\1\s\w{1,}((?:\s\w{1,})?)+\s+&\s+\w{1,}\s(((?:\s\w{1,})?)+)\1$/;
然而 JavaScript解释器通过以下简单测试发出“失败”警报:
(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");
})()
我是否误解了 \1 的目的,是否有任何解决方案可以使用正则表达式执行我想要执行的操作,如上所示?感谢您的回复。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
删除正则表达式开头的 \1。之后它仍然不会报告通过,但这可能是您的正则表达式中的其他错误。我尝试简化您的代码以执行或多或少相同的操作:
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:
此正则表达式将测试是否存在一个名称,后跟任意数量的垃圾,以相同的名称结尾:
This regexp will test if there is a name, followed by an arbitrary amount of garbage, ending in the same name: