Check out Scrivo

Do you want to try out Scrivo? Then here's a demo for you that does not just get your feet wet but lets you plunge right in.

Contact us

For more information, please contact us. We're happy to help you out!

Next Oct 25 Previous

Top 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


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).