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
  • 7,945
  • 12
  • 54
  • 78
  • 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
  • 45,845
  • 14
  • 70
  • 113
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
  • 713,503
  • 59
  • 514
  • 593