Welcome to Bucaro TecHelp!

Bucaro TecHelp
HTTPS Encryption not required because no account numbers or
personal information is ever requested or accepted by this site

About Bucaro TecHelp About BTH User Agreement User Agreement Privacy Policy Privacy Site Map Site Map Contact Bucaro TecHelp Contact RSS News Feeds News Feeds

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.

RSS Feed RSS Feed

Follow Stephen Bucaro Follow @Stephen Bucaro



Fire HD
[Site User Agreement] [Privacy Policy] [Site map] [Search This Site] [Contact Form]
Copyright©2001-2019 Bucaro TecHelp 13771 N Fountain Hills Blvd Suite 114-248 Fountain Hills, AZ 85268