In the case below where a parrent is to be extracted from a string via a regular expression
input_string = "02150 ESPOO"
input_string[pattern]
Two very different result emerge whether the pattern regex is bookended (starting and finishing) /^\d{5}$/ or
not /\d{5}/
I get the desired result in the latter case, while the former returns nil.
Why does the bookeneded regex fail?
Answer
The first expression /^\d{5}$/ starts with ^ (which matches the start of a line) and $ (which matches the end of a line). The whole expression can be described as: Match a string, that
- starts a new line
- contains exactly 5 digits only
- ends the line.
This string doesn't match the string you provided, because the string includes two segments one with 5 digits at the start of the string but then there is a unmatched whitespace and 5 letters before the end of the line.
You may try /^\d{5}/ instead (which matches 5 digits at the start of a line).


![Next-js App-Router dynamic route: ‘params should be awaited’ warning in /api/…/[id]/… handler — why and how to fix?](/_next/image?url=https%3A%2F%2Fcdn.sanity.io%2Fimages%2F80wy3gkl%2Fproduction%2F3a6e81eb886bdc1ee75a15416d662d0f5bc7ea85-1280x720.png&w=3840&q=75)