常规Exp查找重复短语2个连续单词

常规Exp查找重复短语2个连续单词

本文介绍了常规Exp查找重复短语2个连续单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想捕捉2个按顺序重复出现的单词

I would like to catch 2 words sequentially duplicated in a pattern

在这种情况下,我希望能够捕获重复单词"cancer cervical",因为它们是顺序上唯一重复的2个单词.

I would like to be able to catch duplicate words in this case "cancer cervical" because they are the only duplicate 2 words sequentially.

模式"cancer cervical diane mortality cervical cancer cervical diane sea"

我使用此正则表达式,但仍无法捕获2个顺序重复的单词.

I use this Regular expression but still can't catch 2 sequentially duplicated words.

/(\W|^)(.+)\s\2/ig

推荐答案

这应该可以完成工作:/(\w+\s\w.*)\s.*\1/如果要处理包含数字的单词.

This should be able to do the job: /(\w+\s\w.*)\s.*\1/ if you are dealing with words including numbers.

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));

这篇关于常规Exp查找重复短语2个连续单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 05:12