Sunday, November 17, 2024
Google search engine
HomeLanguagesJavascriptJavaScript RegExp B Metacharacter

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. 

Javascript




function geek() {
    let str1 = "neveropen@_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”. 

Javascript




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:

  • Chrome
  • Safari
  • Firefox
  • Opera
  • Edge

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.  

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments