JavaScript RegExp b and B Metacharacter

The regular expression "\b" identifies the presence of the start or end of a word within a given string in JavaScript. Conversely, the regular expression "\b" detects the absence of the beginning or end of a word in a JavaScript string. It enables us to evaluate, search, and find occurrences of a specific word within the string.

Syntaxes

JavaScript "\b" regex syntax

The syntax is utilized to display the characters that are accessible at the beginning of the string.

Example

/\bRegex/

The syntax indicates the character that is present at the conclusion of the string.

Example

/Regex\b /

JavaScript "\B" regex syntax

The syntax displays an invalid character at the beginning of the string.

Example

/\BRegex/

The syntax displays the character that is not present at the conclusion of the string.

Example

/Regex\B /

Examples

JavaScript "\b" regex Examples

The provided examples demonstrate how the character's presence can be determined at the beginning or end of a string.

Example1

The following example demonstrates how to check if a specific character is present at the beginning of a string.

Example

<!DOCTYPE html>

<html>

<head>

<title>

JavaScript RegExp \b Metacharacter

</title>

<style>

#demo1{

background-color: orange;

border: 1px solid black;

width: 350px;

}

</style>

</head>

<body>

<div id = "demo1">

<h3>

JavaScript RegExp \b Metacharacter 

</h3>

<h4> The search method with the "g" modifier get the position of the "po" character in the beginning.

</h4>

<div id = "regex_information"> </div>

<!-- click the button to get the output -->

<button onclick = "display();"> Click Here ! </button>

</div>

<script>

//Get the position of the value at the start position

function display() {

let input_pattern = /\bpo/g;

var val1 = 'T point Tech';

var val2 = 'Example';

var val3 = 'java point';

//use regex search method for pattern 

var t1 = val1.search(input_pattern); 

var t2 = val2.search(input_pattern); 

var t3 = val3.search(input_pattern); 

//the output shows the availability of the value 

var ele = document.getElementById("regex_information");

ele.innerHTML = "T point Tech : "+t1+"<br> Result: "+t2+" <br> java point : "+t3;

}

</script>

</body>

</html>

Output

The displayed result indicates the specific character needed at the initial position.

Example2

The demonstration illustrates the presence of the necessary character at the conclusion of the string.

Example

<!DOCTYPE html>

<html>

<head>

<title>

JavaScript RegExp \b Metacharacter

</title>

<style>

#demo1{

background-color: orange;

border: 1px solid black;

width: 350px;

}

</style>

</head>

<body>

<div id = "demo1">

<h3>

JavaScript RegExp \b Metacharacter 

</h3>

<h4> The search method with the "g" modifier gets the position of the "nt" character in the ending position.

</h4>

<div id = "regex_information"></div>

<!-- click the button to get the output -->

<button onclick = "display();"> Click Here ! </button>

</div>

<script>

//Get the position of the value at the end position

function display() {

let input_pattern = /nt\b/g;

var val1 = 'T point Tech';

var val2 = 'Example';

var val3 = 'java C# Tutorial';

var t1 = val1.search(input_pattern); 

var t2 = val2.search(input_pattern); 

var t3 = val3.search(input_pattern); 

//the output shows the position of the value 

var ele = document.getElementById("regex_information");

ele.innerHTML = "T point Tech : "+t1+"<br> Result: "+t2+" <br> java Result: "+t3;

}

</script>

</body>

</html>

Output

The displayed output indicates the character that is needed to be at the beginning position.

Example3

The sample tests for the specified character can be found at the beginning of the string, considering the input value's case sensitivity.

Example

<!DOCTYPE html>

<html>

<head>

<title>

JavaScript RegExp \b Metacharacter

</title>

<style>

#demo1{

background-color: orange;

border: 1px solid black;

width: 350px;

}

</style>

</head>

<body>

<div id = "demo1">

<h3>

JavaScript RegExp \b Metacharacter 

</h3>

<h4> The match method with the "g" modifier gets the position of the "java" character not in the beginning.

</h4>

<div id = "regex_information"> </div>

<!-- click the button to get the output -->

<button onclick = "display();"> Click Here! </button>

</div>

<script>

//Get the value using the match method at the start position

function display() {

let input_pattern = /\bjava/g;

var val1 = 'T point Tech';

var val2 = 'Example';

var val3 = 'java point';

//use regex match method for pattern 

var t1 = val1.match(input_pattern); 

var t2 = val2.match(input_pattern); 

var t3 = val3.match(input_pattern); 

//the output shows the position of the value 

var ele = document.getElementById("regex_information");

ele.innerHTML = "T point Tech : "+t1+"<br> Result: "+t2+" <br> java point : "+t3;

}

</script>

</body>

</html>

Output

The output displays the sensitivity to uppercase and lowercase characters at the starting position.

JavaScript "\B" regex Examples

The illustrations demonstrate the absence of character availability at the beginning or end of the string.

Example1

The provided example demonstrates that the necessary character is not present at the beginning of the string.

Example

<!DOCTYPE html>

<html>

<head>

<title>

JavaScript RegExp \B Metacharacter

</title>

<style>

#demo1{

background-color: orange;

border: 1px solid black;

width: 350px;

}

</style>

</head>

<body>

<div id = "demo1">

<h3>

JavaScript RegExp \B Metacharacter 

</h3>

<h4> The search method with the "g" modifier gets the position of the "va" character, not in the ending position.

</h4>

<div id = "regex_information"></div>

<!-- click the button to get the output -->

<button onclick = "display();"> Click Here ! </button>

</div>

<script>

function display() {

// use not available po value at the start of the position

let input_pattern = /\Bpo/g;

var val1 = 'T point Tech';

var val2 = 'Example';

var val3 = 'java point';

//use the search method for the input pattern 

var t1 = val1.search(input_pattern); 

var t2 = val2.search(input_pattern); 

var t3 = val3.search(input_pattern); 

//the output shows the not available position of the value 

var ele = document.getElementById("regex_information");

ele.innerHTML = "T point Tech : "+t1+"<br> Result: "+t2+" <br> java point : "+t3;

}

</script>

</body>

</html>

Output

The output displays the necessary character, but it is not located at the initial position.

Example2

The illustration demonstrates the absence of the necessary character at the conclusion of the string. It is possible to utilize a regular expression or the specific character to locate the end position.

Example

<!DOCTYPE html>

<html>

<head>

<title>

JavaScript RegExp \B Metacharacter

</title>

<style>

#demo1{

background-color: orange;

border: 1px solid black;

width: 350px;

}

</style>

</head>

<body>

<div id = "demo1">

<h3>

JavaScript RegExp \B Metacharacter 

</h3>

<h4> The search method with the "g" modifier gets the position of the "va" character, not in the ending position.

</h4>

<div id = "regex_information"></div>

<!-- click the button to get the output -->

<button onclick = "display();"> Click Here ! </button>

</div>

<script>

function display() {

// use not available va value at the end of the position

let input_pattern = /va\B/g;

var val1 = 'T point Tech';

var val2 = 'Example';

var val3 = 'java point';

//use the search method for the input pattern 

var t1 = val1.search(input_pattern); 

var t2 = val2.search(input_pattern); 

var t3 = val3.search(input_pattern); 

//the output shows not available end position of the value 

var ele = document.getElementById("regex_information");

ele.innerHTML = "T point Tech : "+t1+"<br> Result: "+t2+" <br> java point : "+t3;

}

</script>

</body>

</html>

Output

The output displays the necessary character rather than the final position.

Example3

The sample tests are unavailable for the specified character at the beginning of the string when considering case sensitivity in the input. The lowercase input does not cover testing the uppercase character in the string.

Example

<!DOCTYPE html>

<html>

<head>

<title>

JavaScript RegExp \B Metacharacter

</title>

<style>

#demo1{

background-color: orange;

border: 1px solid black;

width: 350px;

}

</style>

</head>

<body>

<div id = "demo1">

<h3>

JavaScript RegExp \B Metacharacter 

</h3>

<h4> The match method with the "g" modifier gets the position of the "java" character not in the beginning.

</h4>

<div id = "regex_information"></div>

<!-- click the button to get the output -->

<button onclick = "display();"> Click Here ! </button>

</div>

<script>

function display() {

// use not available java value at the start of the position

let input_pattern = /\Bjava/g;

var val1 = 'T point Tech';

var val2 = 'Example';

var val3 = 'java point';

//use the match method for the input pattern 

var t1 = val1.match(input_pattern); 

var t2 = val2.match(input_pattern); 

var t3 = val3.match(input_pattern); 

//the output shows a null value 

var ele = document.getElementById("regex_information");

ele.innerHTML = "T point Tech : "+t1+"<br> Result: "+t2+" <br> java point : "+t3;

}

</script>

</body>

</html>

Output

The output demonstrates the sensitivity to letter case at the starting position.

Conclusion

The metacharacters "\b" and "\B" in regular expressions indicate the beginning and ending positions of a value.

Input Required

This code uses input(). Please provide values below: