Wednesday, July 3, 2024
HomeLanguagesJavascriptJavaScript RegExp b Metacharacter

JavaScript RegExp \b Metacharacter

The RegExp \b Metacharacter in JavaScript is used to find a match 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 “neveropen” at the beginning of the string. 

Javascript




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


Output

Found 1 match: neveropen

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

Javascript




function geek() {
    let str1 = "Geeky@128";
    let regex4 = new RegExp("\\bGeeky", "gi");
    let replace = "GFG";
    let match4 = str1.replace(regex4, replace);
    console.log(" New string: " + match4);
}
geek();


Output

 New string: GFG@128

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

  • Google Chrome
  • Apple Safari
  • Mozilla Firefox
  • Opera
  • Internet Explorer

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!

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments