Word Boundary Anchor/Assertion


A word boundary assertion checks if the current position in the string is a word boundary. A word boundary is between two characters where one is a word character (\w) and the other is not. It can also occur at the beginning or end of a string. You can specify that a match occurs at word boundary with the escape sequence'\b'. A word boundary exists at

  • At the start of a string or new line if it begins with a ASCII word character \w (alphabet, digit or underscore). In the string ‘Word Boundary’, ‘W’ is at word boundary
  • At the end of a string if the last character is a ASCII word character. In the string ‘Word Boundary’, ‘y’ is at word boundary
  • Between a word and a non word character like spaces, punctuation marks etc. In the string ‘The Mechanic’s Manual’, the first ‘e’, the first ‘M’, the second ‘c’, ‘s’ and the second ‘M’ are at word boundaries

A word boundary assertion is a zero width assertion as it does not consume any characters

In the string, ‘The following dashes are at word boundary—-‘, the first ‘-‘ is at a word boundary but the last one is not because even though it is at the end of the string, the last character is not a word character. Ofcourse, the middle two dashes are not at word boundaries.

In the string ‘—- The preceeding dashes are not at word boundary’, none of the dashes are at a word boundary.

In the string ‘The following plusses++++are at world boundaries’, the first and the lase ‘+’ signs are at the word boundaries.

let wordPttn = /\b[-]+/;
let targetString = "The following dashes are at word boundary----";
let result = targetString.match(wordPttn);
console.log((result !== null ?
`Pattern ${wordPttn} FOUND in '${targetString}'. Matched string is '${result[0]}'`
: `Pattern ${wordPttn} NOT FOUND in '${targetString}'`));
wordPttn = /\b[-]+/;
targetString = "---- The preceeding dashes are not at word boundary";
result = targetString.match(wordPttn);
console.log((result !== null ?
`Pattern ${wordPttn} FOUND in '${targetString}'. Matched string is '${result[0]}'`
: `Pattern ${wordPttn} NOT FOUND in '${targetString}'`));
wordPttn = /\b[+]+\b/;
targetString = "The following plusses++++are at world boundaries";
result = targetString.match(wordPttn);
console.log((result !== null ?
`Pattern ${wordPttn} FOUND in '${targetString}'. Matched string is '${result[0]}'`
: `Pattern ${wordPttn} NOT FOUND in '${targetString}'`));

Output:

Pattern /\b[-]+/ FOUND in 'The following dashes are at word boundary----'. Matched string is '----'
Pattern /\b[-]+/ NOT FOUND in '---- The preceeding dashes are not at word boundary'
Pattern /\b[+]+\b/ FOUND in 'The following plusses++++are at world boundaries'. Matched string is '++++'

If you want to match colour as a word by itself and not ‘colouring’, ‘redcolour’, ‘coloured’, you can use the pattern /\bcolour\b/.

let wordPttn = /\bcolour\b/;
let targetString = "Red is a primary colour";
let result = targetString.match(wordPttn);
console.log((result !== null ?
`Pattern ${wordPttn} FOUND in '${targetString}'. Matched string is '${result[0]}'`
: `Pattern ${wordPttn} NOT FOUND in '${targetString}'`));
targetString = "Red is one of the primary colours";
result = targetString.match(wordPttn);
console.log((result !== null ?
`Pattern ${wordPttn} FOUND in '${targetString}'. Matched string is '${result[0]}'`
: `Pattern ${wordPttn} NOT FOUND in '${targetString}'`));
targetString = "redcolour";
result = targetString.match(wordPttn);
console.log((result !== null ?
`Pattern ${wordPttn} FOUND in '${targetString}'. Matched string is '${result[0]}'`
: `Pattern ${wordPttn} NOT FOUND in '${targetString}'`));
Pattern /\bcolour\b/ FOUND in 'Red is a primary colour'. Matched string is 'colour'
Pattern /\bcolour\b/ NOT FOUND in 'Red is one of the primary colours'
Pattern /\bcolour\b/ NOT FOUND in 'redcolour'

Note that \b inside a character class (square brackets [\b]) matches a literal backspace and not a word boundary.