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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4541573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 14:02:57  来源:igfitidea点击:

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

javascriptregexword-boundaryboundaryword-boundaries

提问by DarkLightA

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

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

回答by Mark Byers

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

单词边界 ( \b) 是零宽度匹配,可以匹配:

  • Between a word character (\w) and a non-word character (\W) or
  • Between a word character and the start or end of the string.
  • 在单词字符 ( \w) 和非单词字符 ( \W) 之间或
  • 在单词字符和字符串的开头或结尾之间。

In Javascript the definition of \wis [A-Za-z0-9_]and \Wis anything else.

在 Javascript 中,\wis[A-Za-z0-9_]\Wis的定义是其他任何东西。

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

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

  • 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.
  • 在两个单词字符之间。
  • 在两个非单词字符之间。
  • 在非单词字符和字符串的开头或结尾之间。
  • 空字符串。

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

例如,如果字符串"Hello, world!",然后\b在以下地方匹配:

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

And \Bmatches those places where \bdoesn't match:

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

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

回答by derenio

The basic purpose of non-word-boundaryis to created a regex that says:

的基本目的non-word-boundary是创建一个正则表达式,它说:

  • if we are at the beginning/end of a word char(\w= [a-zA-Z0-9_]) make sure the previous/next character is also a word char,

    e.g.: "a\B."~ "a\w":

    "ab", "a4", "a_", ... but not "a ", "a."

  • if we are at the beginning/end of a non-word char(\W= [^a-zA-Z0-9_]) make sure the previous/next character is also a non-word char,

    e.g.: "-\B."~ "-\W":

    "-.", "- ", "--", ... but not "-a", "-1"

  • 如果我们在 a word char( \w= [a-zA-Z0-9_])的开头/结尾,请确保前一个/下一个字符也是 a word char

    例如:"a\B.""a\w"

    "ab", "a4", "a_", ... 但不是"a ","a."

  • 如果我们在 a non-word char( \W= [^a-zA-Z0-9_])的开头/结尾,请确保前一个/下一个字符也是 a non-word char

    例如:"-\B.""-\W"

    "-.", "- ", "--", ... 但不是"-a","-1"

For word-boundaryit's similar but instead of making sure that the adjacent characters are of the same class (word char/non-word car) they need to differ, hence the name word's boundary.

因为word-boundary它是相似的,但不是确保相邻字符属于同一类(word char/ non-word car),它们需要不同,因此名称word's boundary.