1. Tech

Your suggestion is on its way!

An email with a link to:


was emailed to:

Thanks for sharing About.com with others!

Regular Expressions

13. Boundaries

clr gif

Earlier in this tutorial series we saw how to add boundary tests for the start and end of the text string to a regular expression through the use of ^ and $ to represent the start and end of the string respectively. There are two more escape characters that relate to boundary tests that allow you to test for and extract words from within a regular expression.

var re = /\bthe\b/ig;
var myString = 'The boy sat over there on the log. Then he went up the tree.';

By specifying that "the" must start and end with a word boundary this regular expression now only matches the "The" at the very start of the string and the "the" just before the word "tree". It does not match with the first three letters of the words "there" or "then" as would be the case if the word boundaries were not included in the expression. A word boundary can be either whitespace or the start or end of the string so \b is equivalent to ^|\s|$.

You can also test for text that is contained within a word but not at the start or end of the word by using the \B non-word boundary test. For example if your expression is /\Bthe\b/ you are testing for words that end with "the" but which have at least one other character before the "t".

There is one other use for \b where it has a completely different meaning from the word boundary condition described here. If the \b appears within [] then it represents a single backspace character and not a word boundary.

©2016 About.com. All rights reserved.