I'm currently learning regular expressions, and I'd appreciate any help understanding this:
Suppose I have a string with leading and trailing whitespace:
abc
and I would like to isolate the string while removing all of the whitespace. My idea is to use:
\s*(\w+)\s*
as the * quantifier, being greedy, will take as much of the leading and trailing whitespace as it can, which leaves me with just the string "abc". This approach seems to work.
However, I have seen some solutions have the ^ and $ anchors included, giving:
^\s*(\w+)\s*$
Why is it necessary to include the ^ and $ anchors? I know their function, however I can't see why the \s* preceding and following the (\w*) is not sufficient.
" a b c "