I am reading a book on regular expression and I came across this example for \b
:
The cat scattered his food all over the room.
Using regex - \bcat\b
will match the word cat
but not the cat
in scattered
.
For \B
the author uses the following example:
Please enter the nine-digit id as it
appears on your color - coded pass-key.
Using regex \B-\B
matches -
between the word color - coded
. Using \b-\b
on the other hand matches the -
in nine-digit
and pass-key
.
How come in the first example we use \b
to separate cat
and in the second use \B
to separate -
? Using \b
in the second example does the opposite of what it did earlier.
Please explain the difference to me.
EDIT: Also, can anyone please explain with a new example?
With a different example:
Consider this is the string and pattern to be searched for is 'cat':
text = "catmania thiscat thiscatmaina";
Now definitions,
'\b' finds/matches the pattern at the beginning or end of each word.
'\B' does not find/match the pattern at the beginning or end of each word.
Different Cases:
Case 1: At the beginning of each word
result = text.replace(/\bcat/g, "ct");
Now, result is "ctmania thiscat thiscatmaina"
Case 2: At the end of each word
result = text.replace(/cat\b/g, "ct");
Now, result is "catmania thisct thiscatmaina"
Case 3: Not in the beginning
result = text.replace(/\Bcat/g, "ct");
Now, result is "catmania thisct thisctmaina"
Case 4: Not in the end
result = text.replace(/cat\B/g, "ct");
Now, result is "ctmania thiscat thisctmaina"
Case 5: Neither beginning nor end
result = text.replace(/\Bcat\B/g, "ct");
Now, result is "catmania thiscat thisctmaina"
Hope this helps :)
The metacharacter \b is an anchor like the caret and the dollar sign. It matches at a position that is called a "word boundary". This match is zero-length.
There are three different positions that qualify as word boundaries:
\B is the negated version of \b. \B matches at every position where \b does not. Effectively, \B matches at any position between two word characters as well as at any position between two non-word characters.
Source: http://www.regular-expressions.info/wordboundaries.html
\B
is not\b
e.g. negative\b
pass-key
here is no word boundary beside-
so it matches\B
in your first example there are word boundary beside cat so it matches\b
similar rules apply for others too.
\W
is negative of\w
\UPPER CASE
is negative of\LOWER CASE
The confusion stems from your thinking
\b
matches spaces (probably because "b" suggests "blank").\b
matches the empty string at the beginning or end of a word.\B
matches the empty string not at the beginning or end of a word. The key here is that "-" is not a part of a word. So<left>-<right>
matches\b-\b
because there are word boundaries on either side of the-
. On the other hand for<left> - <right>
(note the spaces), there are not word boundaries on either side of the dash. The word boundaries are one space further left and right.On the other hand, when searching for
\bcat\b
word boundaries behave more intuitively, and it matches " cat " as expected.Let take a string like :
Note: Underscore ( _ ) is not considered a special character in this case.
/\bX\b/g
Should begin and end with a special character or white Space/\bX/g
Should begin with a special character or white Space/X\b/g
Should end with a special character or white Space/\BX\B/g
Should not begin and not end with a special character or white Space
/\BX/g
Should not begin with a special character or white Space/X\B/g
Should not end with a special character or white Space/\bX\B/g
Should begin and not end with a special character or white Space/\BX\b/g
Should not begin and should end with a special character or white Space\b
matches a word-boundary.\B
matches non-word-boundaries, and is equivalent to[^\b]
(?!\b)
(thanks to @Alan Moore for the correction!). Both are zero-width.See http://www.regular-expressions.info/wordboundaries.html for details. The site is extremely useful for many basic regex questions.