gpt4 book ai didi

Javascript 正则表达式引擎 : Word boundaries not matching at start of string for non-word characters

转载 作者:行者123 更新时间:2023-11-29 10:02:29 24 4
gpt4 key购买 nike

我认为 \b 匹配单词和非单词字符之间的过渡,或者匹配字符串的开头或结尾。所以这应该是真的:

'#abc'.match(/\b#/)

但它是空的,至少在 Firefox 和 Chrome 中是这样。知道为什么吗?

最佳答案

\b 等同于 (^\w|\w$|\W\w|\w\W)。您可能已经从 the mozilla documentation 中阅读了以下内容:

A word boundary matches the position between a word character followed by a non-word character, or between a non-word character followed by a word character, or the beginning of the string, or the end of the string.

写得不对。它应该指定它匹配字符串的开头或结尾当与单词字符相邻时。这就是在试图解释一些相当算法的东西时写长句子而不是使用要点的问题:它很难阅读,因此也很难校对。


来自 a source other than mozilla 的正确定义示例:

There are three different positions that qualify as word boundaries:

  • Before the first character in the string, if the first character is a word character.
  • After the last character in the string, if the last character is a word character.
  • Between two characters in the string, where one is a word character and the other is not a word character.

关于Javascript 正则表达式引擎 : Word boundaries not matching at start of string for non-word characters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52655367/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com