Find One or More Criminals in a Hunt- greedy regex

Tell us what’s happening:
I am confused about the instructions for this challenge.
The instructions indicate that we should use the “greedy” regex. I’m assuming that we have to use the “ * ” operator, so I solved the challenge with the following solution:

let reCriminals = /[C+]*C/;

I don’t understand why it is also correct to use:

let reCriminals = /C+/;

since this solution does not include de “ * “ operator.

Your code so far


let reCriminals = /[C+]*C/; // Change this line

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36.

Challenge: Find One or More Criminals in a Hunt

Link to the challenge:

Greedy implies finding the longest match. It’s not about the *

If you write just /C/g it will be the number of criminals. But we want just to know if there is one or more. So C, CC, CCC should return C. That’s what greedy refers here.


Here is an example to test:

let test = "ABCC"
let reCriminals = /C+/; // Change this line
let match = test.match(reCriminals)
console.log(match)

Edit: a few details
The regex you copied is somehow saying the same thing. But it really means:
/[C+]*C/ match 0 or more occurrences of C or + previous to a letter C
The problem is this regex would match some extra bits:


let test = "+CC"

let reCriminals = /[C+]*C/; // Change this line
let match = test.match(reCriminals)
console.log(match)

Returns +CC :slight_smile:

1 Like

this means “one or more C”

this means “zero or more characters from the character class [C+] (character class means any of the characters includes, so C or +) followed by one C”
which is a bit unnecessary

1 Like

Thank you @anon10002461 @ilenia , now I understand that including the + would return an incorrect value.