Open In App

JavaScript RegExp \B Metacharacter

The RegExp \B Metacharacter in JavaScript is used to find a match that is not present at the beginning or end of a word. If a match is found it returns the word else it returns NULL. 

Syntax: 



/\B/ 

or

new RegExp("\\B")

Syntax with modifiers:



/\B/g 

or

new RegExp("\\B", "g")

Example 1: This example matches the word “for” which is not present at the beginning or end of the word. 




function geek() {
    let str1 = "GeeksforGeeks@_123_$";
    let regex4 = /\Bfor/gi;
    let match4 = str1.match(regex4);
 
    console.log("Found " + match4.length
        + " match: " + match4);
}
geek();

Output
Found 1 match: for

Example 2: This example matches the word “Geeky” and replaces it with “GEEKY”. 




function geek() {
    let str1 = "123geeky456";
    let regex4 = new RegExp("\\Bgeeky", "gi");
    let replace = "GEEKY";
    let match4 = str1.replace(regex4, replace);
    console.log(" New string: " + match4);
}
geek();

Output
 New string: 123GEEKY456

Supported Browsers: The browsers supported by RegExp \B Metacharacter are listed below:

We have a complete list of Javascript RegExp expressions, to check those please go through this JavaScript RegExp Complete Reference article.

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.  

Article Tags :