Next Oct 25 PreviousTop not stop
When you want your regular expression to match word boundaries which metacharacter can be used?
A: \w
B: \W
C: \b
D: \B
Answer
When you want to match words ending with "top" like "stop" you can use the word boundary metacharacter \b. The expression /top\b/
will find the "top" in "a stop" but not in "the topping". Putting the \b
on the beginning of the expression obviously does the reverse. Answer C is therefore correct.
\B
is the pattern for a 'not a word boundary'. So the pattern /top\B/
will not result in a match on "stop it" but it will match "stopping it".
\w
matches any word character and \W
any non word character. So they do not look for word boundaries but they can nicely work together with the word boundary metacharacter: /\w+top\b/
matches all complete words that end with "top" (excluding "top" itself).