Menu
Regular Expressions Boundaries

A Regular Expression (regex) is a sequence of characters that define a pattern that allows you to search, match, locate, replace, manipulate, and manage text. I explained the basics of Regular Expressions in an earlier article: What is a Regular Expression?

A Boundary controls where a regular expression matches a string, on a word or non-word boundary, or at the beginning or end of a string, or at the beginning or end of a line.

Word Boundary Control Character

The \b control character starts matching at:

a boundary between a non-alpha character and an alpha character,
or a boundary between an alpha character and a non-alpha character,
or a beginning or end of a string (called zero-length character).

These are referred to as word boundaries.

The example below replaces "people" in the word "peopleare" because it finds the zero-length character at the beginning of the string and it finds the non-alpha character (space) after "peopleare".

<script>
let strTarget = "peopleare governed by thepeoplefor thepeople2";
let res = strTarget.replace(/\bpeople/g, "trees");
alert(res);
</script>

This example displays "treesare governed by thepeoplefor thepeople2" in a message box.

The example below replaces "people" at the end of the string because it finds the boarder between the word "people" and the zero-length character end of the string.

<script>
let strTarget = "thepeopleare governed by thepeoplefor people";
let res = strTarget.replace(/\bpeople/g, "trees");
alert(res);
</script>

This example displays "thepeopleare governed by thepeoplefor trees" in a message box.

The example below replaces the word "people" in the middle of the string because it finds the non-alpha character (space) before "people".

<script>
let strTarget = "thepeopleare governed by people thepeople2";
let res = strTarget.replace(/\bpeople/g, "trees");
alert(res);
</script>

This example displays "thepeopleare governed by trees thepeople2" in a message box.

The NOT Word Boundary Control Character

The \B control character is the opposite of \b. \B matches at every position where \b does not.

The example below replaces the word "people" in "thepeoplefor" and in "thepeople2".

<script>
let strTarget = "peopleare governed by thepeoplefor thepeople2";
var res = strTarget.replace(/\Bpeople/g, "trees");
alert(res);
</script>

This example does NOT replace "people" in "peopleare" because unlike \b, \B ignores the boundary between a zero-length character and a alpha character at the beginning of a string.

It does replace "people" in the word "thepeoplefor" because unlike \b, \B does start matching at a boundary between two alpha characters.

Similarly, it does replace "people" in "thepeople2" at the end of the string because unlike \b, \B does start matching at a boundary between two alpha characters.

This example displays "peopleare governed by thetreesfor thetrees2" in a message box.

Multiline Mode

The m modifier sets multiline mode. The example below matches "the" at the beginning of each line.

<script>
let strTarget = "the people are governed by\nthe people for\nthe people";
let regexp = /^the/gm;
let res = strTarget.match(regexp);
alert(res);
</script>

Strings are delimited by a \n (newline) character. When in multiline mode It will understand the beginning (^) and end ($) characters to mean the beginning or end of each line of a string.

As in any regular expression, without the "g" (global) control character it will stop after the first match. Without the "i" (case-insensitive) control character it will be a case-sensitive match.

This example displays "the,the,the" in the message box.

In this article you learned how to controls where a regular expression matches a string, on a word or non-word boundary, at the beginning or end of a string. You also learned about multiline mode.


Learn more at amazon.com

More Java Script Code:
• Easy Slide Show Code with Mouseover Pause
• Regular Expression Position Matching
• How to Use HTML5 canvas arc and arcTo Functions
• Let Your Web site Visitors Set Font Size
• Object-Oriented JavaScript
• JavaScript Code to Add or Remove Specific Elements of an Array
• Easy JavaScript FileReader Code
• A Cross-Browser Solution for Handling JavaScript Events
• Easy Fading Text Banner Code
• Date Picker For Your Web Site