5

I know most regular expression engines, including the one in JavaScript have \b to match a word boundary, be it at either the start or end of a word.

But Vim also has two more specific regular expression atoms:

Does JavaScript have an equivalent to these atoms, and if not is there a way to express their more precise semantics some other way?

hippietrail
  • 15,848
  • 18
  • 99
  • 158
  • Actually I just started to worry that `\<` and `\>` are actually from Vim... maybe they're from both. Checking now... – hippietrail Nov 19 '12 at 12:25
  • But won't any word boundary be both the start of some word and the end of another word? What is the distinction you're trying to handle? – Mark Reed Nov 19 '12 at 12:27
  • @MarkReed, no, because a word boundary does match only the position directly before/after a word and NOT e.g. the whitespace before/after the word. – stema Nov 19 '12 at 12:30

1 Answers1

7

As far as I know there is nothing predefined. But what you can do is, to add a lookahead to the word boundary, to check if it is the start or the end of the word.

\< would be then \b(?=\w). This checks if after the word boundary a word character is following ==> start of the word. See this as example on regexr

\> would be then \b(?!\w). This checks if after the word boundary not a word character is following ==> end of the word

stema
  • 90,351
  • 20
  • 107
  • 135