skip to content
Alvin Lucillo

Regex char set and digit

/ 1 min read

💻 Tech

Given the string and regex below, what are the matched values?

String: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Regex: \/d[13579]\g

Answer: 11 13 15 17 19

Explanation:

  • /d selects any numbers
  • [13579] selects any characters that matches the characters inside the square bracket.
  • Together, the regex means match any number that is immediately followed by any of 1, 3, 5, 7, or 9.
  • Note that numbers are separated by space, which doesn’t match.