\b
matches a zero-length word boundary, not a space. You're looking for something more like this:
/first\b.\bsecond/.match('first second')
This will match any character (.
) in between first
and second
, as long as there is a word boundary on either side.
However, this is not how word boundaries are usually used (since there is no need to use a zero-length check when you are matching the word boundary itself). \b
is essentially looking for a non-word character after a word character; so, instead, you could just look for a non-word character in-between the t
in first
and s
in second
:
/first\Wsecond/.match('first second')
This is exactly the same as the first example...but, realistically, you probably just want to match whitespace and can use something like this:
/first\ssecond/.match('first second')
@WiktorStribiżew's third example shows the best use of word boundaries (at the beginning and end). This is because you aren't matching anything before or after, so a zero-length test is helpful. Otherwise, the above examples could match something like first secondary
. In the end, I'd use an expression like:
/\bfirst\ssecond\b/.match('first second')