Are there JavaScript equivalents of the Vim regular expression start and end of word atoms “\<” and “\>”?

南笙酒味 提交于 2019-12-06 02:55:48

问题


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:

  • \< matches only the word boundary at the start of a word
  • \> matches only the word boundary at the end of a word

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


回答1:


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



来源:https://stackoverflow.com/questions/13453618/are-there-javascript-equivalents-of-the-vim-regular-expression-start-and-end-of

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!