Word Boundary b
Word Boundary: \b
The word boundary \b corresponds to positions when one of the sides is a word character, and the other one is not. (单词边界\ b对应于一侧是单词字符而另一侧不是单词字符的位置。)
Word boundaries are especially handy when it is necessary to match a sequence of letters or digits on their own. Or it is useful when you want to ensure that they happen at the start or the end of the sequence of characters. (当需要单独匹配字母或数字序列时,单词边界尤其方便。或者,当您想确保它们发生在字符序列的开头或结尾时,这很有用。)
Once the regular expression engine comes across \b, it checks whether the position within the string is a word boundary. (一旦正则表达式引擎遇到\ b ,它将检查字符串中的位置是否是单词边界。)
Three different positions are qualified as word boundaries. They are the following:
At the beginning of the string, if the initial string character is a word character \w. (-在字符串的开头,如果初始字符串字符是单词字符\ w。)
Between two characters within a string, where one is considered a word character, the other- not. (-字符串中的两个字符之间,其中一个被认为是单词字符,另一个不是。)
At the end of the string, in case the last string character is \w. (-在字符串的末尾,以防最后一个字符串字符为\ w。)
For example, a regexp \bW3\b can be found in Welcome to W3 where W3 is considered a standalone word, but not Welcome to w3cdoc as follows:
console.log("Welcome to W3".match(/\bW3\b/)); // W3
console.log("Welcome to w3cdoc".match(/\bW3\b/)); // null
It matches the \bWelcome\b pattern. There are three primary reasons for that:
At the string beginning, the first test \b matches. Then the word Welcome matches. Afterward, the test \b matches again. (在字符串开头,第一个测试\ b匹配。 然后“欢迎”一词匹配。 之后,测试\ b再次匹配。)
The \bW3\b pattern could also match but not \bWell\b, as there isn’t any word boundary after l. The W3\b couldn’t match either because the exclamation sign is not \w. (\ bW3\ b模式也可以匹配,但不能匹配\ bWell\ b ,因为l之后没有任何单词边界。W3\ b也无法匹配,因为感叹号不是\ w。)
Here is an example:
console.log("Welcome to w3cdoc".match(/\bWelcome\b/)); // Welcome
console.log("Welcome to w3cdoc".match(/\bw3cdoc\b/)); // w3cdoc
console.log("Welcome to w3cdoc".match(/\bWell\b/)); // null (no match)
console.log("Welcome to w3cdoc".match(/\bw3cdoc!\b/)); // null (no match)
You can use \b both with words and digits. (您可以将\ b与单词和数字一起使用。)
For instance, the \b\d\d\b pattern searches for standalone two-digit numbers. (例如,\ b\ d\ d\ b模式搜索独立的两位数。)
That is, it searches for two-digit numbers surrounded by characters different from \w (for example, punctuation or spaces), like this;
console.log("1 32 759 10".match(/\b\d\d\b/g)); // 32, 10
console.log("21,45,75".match(/\b\d\d\b/g)); // 21,45,75
Another important thing to note: \b will not work for non-latin alphabets.
So, \w means a Latin letter a-z (an underscore or a digit). Therefore, the test doesn’t work for other characters such as Chinese hieroglyphs, Cyrillic letters, and so on. (因此,\ w表示拉丁字母a-z (下划线或数字)。因此,该测试不适用于其他字符,如中文象形文字、西里尔字母等。)