与字边界相比,正则表达式(\ B)中的非字边界是什么? [英] What are non-word boundary in regex (\B), compared to word-boundary?

查看:237
本文介绍了与字边界相比,正则表达式(\ B)中的非字边界是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与字边界相比,正则表达式(\ B)中的非字边界是什么?

What are non-word boundary in regex (\B), compared to word-boundary?

推荐答案

一个字边界( \b )是一个可以匹配的零宽度匹配:

A word boundary (\b) is a zero width match that can match:


  • 在单词字符( \w )和非单词字符( \W )或
  • 在单词字符与字符串的开头或结尾之间。

  • Between a word character (\w) and a non-word character (\W) or
  • Between a word character and the start or end of the string.

在Javascript中定义 \ w [A-Za-z0-9 _] \ W 是其他任何东西。

In Javascript the definition of \w is [A-Za-z0-9_] and \W is anything else.

\b 的否定版本,写成 \B ,是零宽度匹配,上面的保持。因此它可以匹配:

The negated version of \b, written \B, is a zero width match where the above does not hold. Therefore it can match:


  • 两个单词字符之间。

  • 两个非单词字符之间。

  • 在非单词字符与字符串的开头或结尾之间。

  • 空字符串。

  • Between two word characters.
  • Between two non-word characters.
  • Between a non-word character and the start or end of the string.
  • The empty string.

例如,如果字符串是Hello,world!那么 \ b 在以下地方匹配:

For example if the string is "Hello, world!" then \b matches in the following places:

 H e l l o ,   w o r l d !
^         ^   ^         ^ 

\B 匹配 \b 不匹配的地方:

 H e l l o ,   w o r l d !
  ^ ^ ^ ^   ^   ^ ^ ^ ^   ^

这篇关于与字边界相比,正则表达式(\ B)中的非字边界是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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