DEV Community

Discussion on: Cheatsheet for the Regex Cheatsheet, Part 1: Anchors

Collapse
 
mathlete profile image
Analogy | Absence | Example

Thanks, @inhuofficial ! Can you show me an example of \B Not word boundary using the same format with the sentence ""The lion roared"?

Collapse
 
grahamthedev profile image
GrahamTheDev

Yes so in "The Lion Roared" the matches would be where the pipes are:

T|h|e L|i|o|n R|o|a|r|e|d

and for the original \b the matches would be

|The| |Lion| |Roared|

There is a difference on how this matches that I didn't explain very well, it is between to characters (so at a boundary) that it matches.

So what it gives you is the positions of where the boundaries are. (so instead of returning positions and then the letters matched / groups you will only ever get back the positions (I think, long time since I used it).

I don't think I have ever found a use for \B but \b can be really useful for splitting strings into words.

See: regex101.com/r/yVG7Gb/1 for \b and regex101.com/r/vvGd1d/1 for \B