0

/\Bon/ matches "on" in "at noon", and /ye\B/ matches "ye" in "possibly yesterday". Why does this happen?

As According to the following question https://stackoverflow.com/a/6664167/6575810

As it says

\B is a zero-width non-word boundary. Specifically:

Matches at the position between two word characters (i.e the position between \w\w) as well as at the position between two non-word characters (i.e. \W\W).

It should not. Am I missing anything here ?

Also see my comment on trott's Answer https://stackoverflow.com/a/46439352/6575810

Useless Code
  • 12,123
  • 5
  • 35
  • 40
Ankur Marwaha
  • 1,613
  • 2
  • 15
  • 30

2 Answers2

3

That behavior seems correct. Sounds like you might want \b for word boundaries.

/\Bon/.test('at noon'); // true
/\bon/.test('at noon'); // false

In the first line, \B is a non-word boundary. In other words, it is a boundary where both the thing before and the thing after are either part of the same word or part of the same non-word (like two spaces next to each other).

In the second line, \b needs to be a word boundary for the regex to match. It does not match because it occurs in the middle of a word.

Trott
  • 66,479
  • 23
  • 173
  • 212
  • how does the thing before "on" that is o and thing after "on" that is nothing. is a part of the same word ? – Ankur Marwaha Sep 27 '17 at 04:22
  • @AnkurMarwaha I'm not sure what you are asking... but trying to clear things up for you, in this example there are two words, "at" and "noon". The "on" is part of the word "noon". The word boundaries are before the first "n" and after the last "n". The non-word boundaries would be between the first "n" and the first "o", between that "o" and the next "o" and then between that and the last "n". `/\Bon/` matches the non-word boundary between the 2 "o"s and then the "on". `\B` is a zero-width anchor, meaning, when it is matched nothing is added to the match string that is returned. – Useless Code Sep 27 '17 at 04:45
3

/\Bon/ matches "on" in "at noon", and /ye\B/ matches "ye" in "possibly yesterday". Why does this happen?

That is the correct and expected behavior:

console.log("at noon".match(/\Bon/)[0]);
console.log("possibly yesterday".match(/ye\B/)[0]);

Unlike character classes like \w, which match a single "word" character, or \s that matches a single white space character, \B is anchor point it does not match a character, it instead asserts that that anchor is at a specific place. In the case of \B, it asserts that that anchor is not at a word boundary.

A word boundary would either be a place where a "word character" is next to a white space character or the beginning or end of the string.

So, /\Bon/ effectively means find an "on" that is not at the start of a word. That is why the "on" in "noon" matches; but something like the "on" in "at one" does not:

    console.log("at one".match(/\Bon/));

In the same way, /ye\B/ effectively means find a "ye" that is not at the end of a word. So, the "ye" in "possibly yesterday" matches because it is not at the end of the word, whereas the "ye" at the end of "possibly goodbye" does not:

console.log("possibly goodbye".match(/ye\B/));

It should also be added that \B should not be confused with \b, they have different meanings. \b matches an anchor point at a word boundary. So, if you wanted to find a word that starts with "on" you could use /\bon/:

console.log("one at noon".match(/\bon/)); // finds the "on" in one, but not the "on" in noon

Likewise it can be used to find something at the end of a word:

    console.log("goodbye yesterday".match(/ye\b/)); // finds the "ye" in goodbye, but not the "ye" in yesterday
Useless Code
  • 12,123
  • 5
  • 35
  • 40