1

how to match non-word+word boundary in javascript regex.

"This is, a beautiful island".match(/\bis,\b/) 

In the above case why does not the regex engine match till is, and assume the space to be a word boundary without moving further.

coool
  • 8,085
  • 12
  • 60
  • 80
  • See https://stackoverflow.com/a/10983983/3832970 and especially https://stackoverflow.com/questions/1324676/what-is-a-word-boundary-in-regexes – Wiktor Stribiżew May 02 '18 at 14:55

2 Answers2

2

\b asserts a position where a word character \w meets a non-word character \W or vice versa. Comma is a non-word character and space is as well. So \b never matches a position between a comma and a space.

Also you forgot to put ending delimiter in your regex.

revo
  • 47,783
  • 14
  • 74
  • 117
1

You can use \B after comma that matches where \b doesn't since comma is not considered a word character.

console.log( "This is, a beautiful island".match(/\bis,\B/) )
//=> ["is,"]
anubhava
  • 761,203
  • 64
  • 569
  • 643