Reputation: 669
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.
Upvotes: 0
Views: 152
Reputation: 1074969
JavaScript regular expression objects have no (Casimir et Hippolyte has changed it for you in the question)match
method. You may be thinking of the one on strings:
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");
Upvotes: 4