最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - check if any duplicate words exist in a string using regex - Stack Overflow

programmeradmin0浏览0评论

Using regex in javascript, how to check for the condition when duplicate words are present in a string? The words could be located any where in the string:

Given: "but boys run fast boys are strong" or
"boys boys"

expected result: true, since there are 2 repeats of the word "boys"

Using regex in javascript, how to check for the condition when duplicate words are present in a string? The words could be located any where in the string:

Given: "but boys run fast boys are strong" or
"boys boys"

expected result: true, since there are 2 repeats of the word "boys"

Share Improve this question edited Mar 29, 2017 at 18:46 Fred J. asked Mar 29, 2017 at 18:39 Fred J.Fred J. 6,04913 gold badges60 silver badges113 bronze badges 4
  • You'd be hard pressed to find any tool less suitable for the task than regex. – C3roe Commented Mar 29, 2017 at 18:44
  • Does it have to be made with regex? – kind user Commented Mar 29, 2017 at 18:45
  • No, but regex gives a shorter / elegant and hopefully faster code. – Fred J. Commented Mar 29, 2017 at 18:52
  • "expected result: true" Is requirement of Question to return a Boolean value? – guest271314 Commented Mar 29, 2017 at 19:03
Add a ment  | 

6 Answers 6

Reset to default 4

\b matches word boundaries
\w+ will match 1 or more word characters
( ... ) creates a group for matches
\1 will match the contents of matching group #1.

Putting this together, you want a regex containing \b(\w+)\b.*\b\1\b

Plus appropriate backslash quoting, etc.


@guest27134 pointed out the above is not the plete solution, since the OP wanted true/false, not just the regex:

var result = a_string.match(/\b(\w+)\b.*\b\1\b/g) !== null

Or, even shorter, as suggested by O.P.:

var result = /\b(\w+)\b.*\b\1\b/g.test(myStr)

Taking into consideration that spaces are there after each word

var string = "but boys run fast boys are strong";

var strArray= string.split(" ");

var unique = [];
 for(var i =0; i< strArray.length; i++)
 {
   eval(unique[strArray] = new Object()); 
 }

If you know what word you're testing for duplicates you could use regular expressions like this str.match(/boys/g).length > 1 to test if that word shows up more than once (assuming your string is in the str variable).


Hello, here it is:

var temp = "but boys run fast boys are strong";
var count = (temp.match(/boys/g) || []).length;
console.log(count);


I hope I have helped!

You can count the occurrence of each matched word

var str = "but boys run fast boys are strong";
var matches = str.split(/\s/);
var res = matches.map(function(match) {
            return str.match(new RegExp(match, "g")).length;
          });

var bool = res.some(function(len) {return len > 1}));

console.log(bool);

for (var i = 0; i < matches.length; i++) {
  if (res[i] > 1) console.log(matches[i], i);
}

Hello!

Here's an example with the exact answer you need:

var keyword = "boys"
var temp = "but boys run fast boys are strong";
var regex = new RegExp(keyword, "g");
var count = (temp.match(regex) || []).length;

if (count > 0) {
    console.log("true, since there are " + count + " repeats of the word '" + keyword + "'");
} else {
    console.log("false, not found.");
}

I hope I have helped!

发布评论

评论列表(0)

  1. 暂无评论