What Does B in RegEx Mean? Word Boundary and Non-word Boundary Metacharacters (2024)

/ #Regex
What Does B in RegEx Mean? Word Boundary and Non-word Boundary Metacharacters (2)

In regular expressions, “B” is a metacharacter for specifying word boundary. It could be in two forms – the capital letter “B” and the small letter “b”.

Since B (and b) is a metacharacter, you need to escape it to make it work (\b and \B). Otherwise, b or B would be interpreted as an alphabet letter.

In this article, I’ll guide you through those two variations of the “B” character in RegEx. We'll look at what they do and their usage in both RegEx engines and programming.

What We'll Cover

  • What Does the “S” Metacharacter Do?
  • How to Use the “B” Metacharacter in RegEx
    • How to Use the Word Boundary (\b) Metacharacter
    • How to Use the Non-word Boundary (\B) Metacharacter
  • Wrapping Up

The small letter “b” metacharacter stands for a word boundary, and the capital letter “B” stands for a non-word boundary. That's how the pattern for most metacharacters works.

For instance, the small letter “s” is the metacharacter for a space (spacebar, tab, carriage return, new line), and the capital letter “S” is the non-space metacharacter.

You can read more about the s metacharacter here.

The small letter \b word boundary indicates that a pattern is bounded by a non-word character. Non-word characters are all characters apart from numbers, letters, and underscore (_). They are denoted by another metacharacter (\W).

On the other hand, the capital letter \B is a non-word boundary that indicates that a pattern is bounded by a word character. Word characters are letters, numbers, and underscore (_). They are denoted by another metacharacter (\w).

How to Use the “B” Metacharacter in RegEx

The \b metacharacter specifies word boundary and \B specifies non-word boundary. Both reference positions, not the actual characters, but they match different things as they are opposites.

How to Use the Word Boundary (\b) Metacharacter

Here’s how the word boundary (\b) metacharacter works:

For example, in the sentence “One should take care of oneself before takin care of others one by one”, putting \b before the word “one” as the defined pattern would match every of the word “one” preceded by a non-word character (anything apart from letters, numbers and underscore _):What Does B in RegEx Mean? Word Boundary and Non-word Boundary Metacharacters (3)

If I put \b on the other side of the word “one” again, then it would match just the word “one” but not “oneself”:What Does B in RegEx Mean? Word Boundary and Non-word Boundary Metacharacters (4)

And that’s what the word boundary metacharacter does – it lets you match the exact part of a string you want.

Note: I was able to match the word “One” because I turned on the case-insensitive flag.

You also don’t have to use the word boundary metacharacter with one word:What Does B in RegEx Mean? Word Boundary and Non-word Boundary Metacharacters (5)

How to Use the Non-word Boundary (\B) Metacharacter

For the non-word boundary metacharacter (\B), remember it matches anything bounded by a word character.

Here is how I used it to match “for” inside the word “before”:What Does B in RegEx Mean? Word Boundary and Non-word Boundary Metacharacters (6)

And this is how I used it to grab “king” from the word “taking”:What Does B in RegEx Mean? Word Boundary and Non-word Boundary Metacharacters (7)

Now, if you want to match the word “the” inside of “others”, you just need to surround the word “the” with \B in your pattern.

The \b and \B metacharacters would also work fine in a language like JavaScript:

let str = 'One should take care of oneself before taking care of others one by one';let regex1 = /\Bking/;let regex2 = /\bone/;console.log(regex1.exec(str));console.log(regex2.exec(str));

This is the result in the console:What Does B in RegEx Mean? Word Boundary and Non-word Boundary Metacharacters (8)

Wrapping Up

The word boundary and non-word boundary metacharacters help you grab the exact word you need in a string or even in a particular word which comes in handy when implementing several validations for user input.

Happy coding!

ADVERTIsem*nT

ADVERTIsem*nT

ADVERTIsem*nT

What Does B in RegEx Mean? Word Boundary and Non-word Boundary Metacharacters (9)
Kolade Chris

I'm a software developer and tech writer focusing on frontend technologies

If you read this far, thank the author to show them you care.

Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

ADVERTIsem*nT

What Does B in RegEx Mean? Word Boundary and Non-word Boundary Metacharacters (2024)
Top Articles
How to Blog (Even If You Can't Write)
8 Ways to Increase Your Daily Step Count
P.o. Box 806514 Chicago Il 60680
Ach Credit Ftb Mct
It May Surround A Charged Particle Crossword
Truist Sat Hours
The Fat Soluble Vitamins Are Weegy
Magic Seaweed Newport Ri
Costner-Maloy Funeral Home Obituaries
Blak Stellenanzeigen
Reston, VA | Reston Demographics in 2024
Cats For Free Craigslist
Danielle Moodie-Mills Net Worth
Highest-Paying VMware Certifications of 2024: Invest in Your Future
Okta Hendrick Login
Caro Walmart Hair Salon
Projectxyz Employee Portal
Lab-grown 'mini-guts' link Crohn's disease severity to epigenetic changes - DSSJ
What Is a Bank Guarantee?
Gasoline Prices At Sam's Club
Denver Post Replica Login
The Best Massage Guns
Bay Country Crab Supply
Donated Food Value Per Pound 2022
Think Up Elar Level 5 Answer Key Pdf
My Location To Pilot Truck Stop
Motherload Unblocked
Denver Ebiz Tax Center
Interval Training Weegy
Bloomingdales Return Policy | What You NEED TO KNOW
Craigslist Rooms For Rent Winston-Salem Nc
Csl Plasma Wilkes-Barre Township Reviews
Dirtyone 2006
76 Precinct Twitter
Hot Rod Dreamworks & Collision Repair Llc
Joanna Fabric Near Me
East Primesuite Login Com Citrix
Bridger Elementary Logan
Penn State E Wall
L'Hôpital's rule - Conditions, Formula, and Examples
Crexi Vs LoopNet: Which Commercial Real Estate Platform Is For You?
Rapper Slimesito | Real Name, Age, Height, Girlfriend, Net Worth, & Wiki - Simply Who
Princessdk
Utah Veterinarian Dr Artz
World of Warcraft Bringing Back Old Anniversary Rewards for the First Time in 15 Years
Excel Module 4 Sam End Of Module Project 2
5W 1H Method for Problem Solving Explained with Example
Makes A Successful Catch Maybe Crossword Clue
Results & Winning Numbers for Last Year - Pick 3 Midday
Qhc Learning
Ltlv Las Vegas
Chelactiv Max Cream
Latest Posts
Article information

Author: Mrs. Angelic Larkin

Last Updated:

Views: 6577

Rating: 4.7 / 5 (67 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Mrs. Angelic Larkin

Birthday: 1992-06-28

Address: Apt. 413 8275 Mueller Overpass, South Magnolia, IA 99527-6023

Phone: +6824704719725

Job: District Real-Estate Facilitator

Hobby: Letterboxing, Vacation, Poi, Homebrewing, Mountain biking, Slacklining, Cabaret

Introduction: My name is Mrs. Angelic Larkin, I am a cute, charming, funny, determined, inexpensive, joyous, cheerful person who loves writing and wants to share my knowledge and understanding with you.