是否存在类似于Vim正则表达式的单词原子的开头和结尾的JavaScript等同于“\<"和“\>”? [英] Are there JavaScript equivalents of the Vim regular expression start and end of word atoms "\<" and "\>"?

查看:120
本文介绍了是否存在类似于Vim正则表达式的单词原子的开头和结尾的JavaScript等同于“\<"和“\>”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道大多数正则表达式引擎,包括JavaScript中的一个,有 \b 来匹配单词边界,无论是在单词的开头还是结尾。

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.

但是Vim还有两个更具体的正则表达式原子:

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

JavaScript是否具有这些原子的等价物,如果没有,是否有办法以其他方式表达更精确的语义?

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

推荐答案

据我所知,没有任何预定义。但你可以做的是,在字边界添加一个 lookahead ,检查是否它是单词的开头或结尾。

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.

\< 然后 \b(?= \w)。这将检查单词边界后面是否有单词字符==>单词的开头。 在regexr上查看此示例

\< 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

\> 然后是 \ b(?!\ w)。这将检查单词边界后面是否有单词字符==>单词结尾

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

这篇关于是否存在类似于Vim正则表达式的单词原子的开头和结尾的JavaScript等同于“\&lt;&quot;和“\&gt;”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆