0

I am supposed to make a regex to match numbers that appear three times in a string each separated by a space. But I don't know how .match() method and capturing group work.

So I have the following:

let repeatNum = "42 42 42";
let reRegex = /(\d+)\s\1/; // Change this line
let result = repeatNum.match(reRegex);
console.log(result);

The result is:

["42 42", "42"]

Ok I kinda understand why the first element of this array is "42 42".

The regex:

/(\d+)\s\1/

means identify one or more number and a space. You put that word in group #1 and say find me another word after that space that is the same as what is group #1.

I have seen how this regex can work in a double-word example. But I don't know how it will work for three or more of the same number?

EDIT: I just found out the result is the same for 42 42 42 42. Now I am more confused.

melpomene
  • 84,125
  • 8
  • 85
  • 148
  • I don't understand what you're trying to ask. There is no question in this post. – melpomene Jun 11 '17 at 16:05
  • Check this [post](https://stackoverflow.com/questions/432493/how-do-you-access-the-matched-groups-in-a-javascript-regular-expression). You may get what you require – Charles Jun 11 '17 at 16:09
  • T. J. Crowder thinks you want to match the whole string only. If he's wrong about that, then you'll need to make sure it doesn't match strings such as `"542 42 429"`. `\b` can help here: `/\b(\d+)\s\1\s\1\b/`. – David Knipe Jun 11 '17 at 16:57

1 Answers1

4

JavaScript regular expression objects have no match method. You may be thinking of the one on strings: (Casimir et Hippolyte has changed it for you in the question)

let repeatNum = "42 42 42";
let reRegex = /(\d+)\s\1/; // Change this line
let result = repeatNum.match(reRegex);
console.log(result);

String#match returns an array containing the overall match as the first entry, followed by the content of any capture groups as subsequent entries in the array. That's why you get ["42 42", "42"]: The "42 42" is the overall match for the expression, and the "42" is the content of the first capture group.

If you just want the overall match, simply use the first entry in the array.

I am supposed to make a regex to match numbers that appear three times in a string each separated by a space.

Your regex won't do that. It will try to match the same number twice in a string.

If you want to match the same number three times, you just need another \s\1:

let repeatNum = "42 42 42";
let reRegex = /(\d+)\s\1\s\1/;
let result = repeatNum.match(reRegex);
console.log(result ? result[0] : "No match");

If you just want to match numbers separated by spaces, the simplest thing is just to use \d+\s\d+\s\d+:

let repeatNum = "42 43 44";
let reRegex = /\d+\s\d+\s\d+/;
let result = repeatNum.match(reRegex);
console.log(result ? result[0] : "No match");

...although you can use \d+(?:\s\d+){2} if you like, which says "A series of digits followed by two instances of: A space followed by a series of digits."

let repeatNum = "42 43 44";
let reRegex = /\d+(?:\s\d+){2}/;
let result = repeatNum.match(reRegex);
console.log(result ? result[0] : "No match");

I just found out the result is the same for 42 42 42 42

Without anchors, the regular expression will find matches within the string; it doesn't require that the match match the entire string. So when you run any of the above against "42 42 42 42", you'll see the same thing as when you use it on "42 42 42", because it matches a substring.

If you want to match only the whole thing, add ^ at the start and $ at the end. Those are the beginning/end of input assertions.

For example, with the first change above (the minimal change to your regex):

let repeatNum = "42 42 42";
let reRegex = /^(\d+)\s\1\s\1$/;
let result = repeatNum.match(reRegex);
console.log("repeatNum:", result ? result[0] : "No match");
let repeatNum2 = "42 42 42 42";
result = repeatNum2.match(reRegex);
console.log("repeatNum2:", result ? result[0] : "No match");
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875