我想在模式中按顺序复制2个单词
我希望能够在这种情况下捕获重复的单词"cancer cervical"
,因为它们是唯一重复的2个单词。
模式"cancer cervical diane mortality cervical cancer cervical diane sea"
我使用这个正则表达式,但仍然无法捕获2个顺序重复的单词。
/(\W|^)(.+)\s\2/ig
答案 0 :(得分:0)
答案 1 :(得分:0)
如果你正在处理包括数字在内的单词,这应该可以完成这项工作:/(\w+\s\w.*)\s.*\1/
。
var string = "cancer cervical diane mortality cervical cancer cervical diane sea";
var regex = /(\w+\s\w.*)\s.*\1/;
console.log(string.match(regex));
var string = "cancer cervical diane mortality cervical cancer cervical diane sea";
var string1=" this is a test cancer cervical cancer diane mortality cervical cancer cervical diane sea";
var string2=" test whether it will catch one word cancer mortality cervical cancer cervical diane sea";
var regex = /(\w+\s\w.*)\s.*\1/;
console.log(string.match(regex));
console.log(string1.match(regex));
console.log(string2.match(regex));