because you are missing the spaces at the beginning and end of string
you wrote /^(\w+) but there are no letters at the beginning of the string , and !$/ but there is not punctuation at the end of string, there are spaces
those stands what you have inside the capture groups
your first capture group is (\w+) so $1 will be what’s matched by the capture group
your regex needs to match the pattern of the string tho, using this way the spaces should be matched, but not captured in the capture groups so that after when you use $1 the spaces are not placed in the new string