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
Chrome now hides notification content when screen sharing to keep alerts private
How to Negotiate Salary with Your Employer and Get the Raise You Deserve
Katie Pavlich Bikini Photos
Gamevault Agent
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Free Atm For Emerald Card Near Me
Craigslist Mexico Cancun
Hendersonville (Tennessee) – Travel guide at Wikivoyage
Doby's Funeral Home Obituaries
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Select Truck Greensboro
Things To Do In Atlanta Tomorrow Night
How To Cut Eelgrass Grounded
Pac Man Deviantart
Alexander Funeral Home Gallatin Obituaries
Craigslist In Flagstaff
Shasta County Most Wanted 2022
Energy Healing Conference Utah
Testberichte zu E-Bikes & Fahrrädern von PROPHETE.
Aaa Saugus Ma Appointment
Geometry Review Quiz 5 Answer Key
Walgreens Alma School And Dynamite
Bible Gateway passage: Revelation 3 - New Living Translation
Yisd Home Access Center
Home
Shadbase Get Out Of Jail
Gina Wilson Angle Addition Postulate
Celina Powell Lil Meech Video: A Controversial Encounter Shakes Social Media - Video Reddit Trend
Walmart Pharmacy Near Me Open
Dmv In Anoka
A Christmas Horse - Alison Senxation
Ou Football Brainiacs
Access a Shared Resource | Computing for Arts + Sciences
Pixel Combat Unblocked
Cvs Sport Physicals
Mercedes W204 Belt Diagram
Rogold Extension
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Teenbeautyfitness
Weekly Math Review Q4 3
Facebook Marketplace Marrero La
Nobodyhome.tv Reddit
Topos De Bolos Engraçados
Gregory (Five Nights at Freddy's)
Grand Valley State University Library Hours
Holzer Athena Portal
Hampton In And Suites Near Me
Stoughton Commuter Rail Schedule
Bedbathandbeyond Flemington Nj
Free Carnival-themed Google Slides & PowerPoint templates
Otter Bustr
Selly Medaline
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.