DEV Community

loading...

Challenge: Regular Expressions

rthefounding profile image Randy Rivera ・1 min read

Find One or More Criminals in a Hunt

  • Let's Write a greedy regex that finds one or more criminals within a group of other people. A criminal is represented by the capital letter C.

  • Here's an example to review how to do this:

  • The regex /z+/ matches the letter z when it appears one or more times in a row. It would find matches in all of the following strings:

"z"
"zzzzzz"
"ABCzzzz"
"zzzzABC"
"abczzzzzzzzzzzzzzzzzzzzzabc"
Enter fullscreen mode Exit fullscreen mode
  • But it does not find matches in the following strings since there are no letter z characters:
""
"ABC"
"abcabc"
Enter fullscreen mode Exit fullscreen mode
  • Answer:
let reCriminals = /C+/;
Enter fullscreen mode Exit fullscreen mode
  • Now your regex should match three criminals (CCC) in the string P1P5P4CCCcP2P6P3.
  • Now your regex should match five criminals (CCCCC) in the string P6P2P7P4P5CCCCCP3P1.

Discussion (0)

Forem Open with the Forem app