Word Boundary



\b indicated the a word boundary. It matches the position between a word and a space. We will now state a practical problem and a solution to better understand the use of the word boundary.

Problem -

Write a Regular Expression, which will match "class" in "Today there is no class for javascript", but not in " This is a classical javascript book".

Solution -

Try the regular expression /\bclass\b/

Here is the complete example
<html>
<body>
<script type="text/javascript">
<!--
/*
********************************************************
Javascript Regular Expression Example 22
Use of word boundary
********************************************************
*/
var pattern1=/\bclass\b/;
var string1 = new Array(4);
string1[0] = "Today there is no class for javascript";
string1[1] = "This is a classical javascript book";
string1[2] = "class for javascript is cancelled today";
string1[3] = "Today there is no javascript class";
var i;
for(i=0; i<=3; i++)
{
if (pattern1.test(string1[i]))
{
document.write(string1[i], " -->matches regular expression","<br\>");
}
else
{
document.write(string1[i], " --> does not match regular expression", "<br\>");
}
}
//-->
</script>
</body>
</html>

Try this Example online


You can try this example online at - here .

If we run the code, it gives out the following output

Today there is no class for javascript -->matches regular expression
This is a classical javascript book --> does not match regular expression
class for javascript is cancelled today -->matches regular expression
Today there is no javascript class -->matches regular expression


We should note that \b is an anchor, similar to ^ and $. It results in a zero length match. We should also note that \b also matches before the start of first word character in a string or after the end of the last character in a string, if the last character is a word character.

Matching a non Boundary \B



If we want to match any position that is not the boundary of a word and non word, we use \B. To consider a practical application, think of a scenario where we want to match all occurrences of the word "is" which is not a complete word. /\Bis\B/ will match "History repeats itself" but not "It is a good book" or "this good book seems expensive". The regular expression /\Bis/ will match "this good book seems expensive" not "It is a good book".

The complete example is here

<html>
<body>
<script type="text/javascript">
<!--
/*
********************************************************
Javascript Regular Expression Example 23
Use of word non boundary using \B
********************************************************
*/
var pattern1=/\Bis\B/ ;
var string1 = new Array(3);
string1[0] = "History repeats itself";
string1[1] = "It is a good book";
string1[2] = "this good book seems expensive";
var i;
for(i=0; i<=2; i++)
{
if (pattern1.test(string1[i]))
{
document.write(string1[i], " -->matches regular expression","<br\>");
}
else
{
document.write(string1[i], " --> does not match regular expression", "<br\>");
}
}
//-->
</script>
</body>
</html>

Try this Example online


You can try this example online at - here .

Here is the output given by this code

History repeats itself -->matches regular expression
It is a good book --> does not match regular expression
this good book seems expensive --> does not match regular expression


We will now write a practical form validation example. In this example inch is converted into centimeter. However, since the user may give a wrong input, we will check the input value to see if a correct input is entered - In the next page