javascript - 通过正则表达式将字符串的开头(空字符串)捕获为匹配本身
问题描述
''.match(/^/)
// ['']
'ab'.match(/\w/g)
// ['a', 'b']
'ab'.match(/^|\w/g)
// ['', 'b']
如您所见,^
被捕获而不是在a
最新示例中,第一个匹配以某种方式是“字符串的开头”而不是“起始字符本身”。
什么正则表达式将从['', 'a', 'b']
字符串中捕获'ab'
?
解决方案
发生了什么?
在 PCRE^|\w
中匹配行首断言^
(空字符串)和单词字符\w
( a
)。
在 JavaScript 中,在字符串^
中的位置满足匹配0
,因此它移动到位置1
(匹配b
),跳过匹配,a
因为该位置已经成功匹配。
我必须确认,但似乎大多数正则表达式引擎(JavaScript、Python、Go 等)每个位置只返回一个匹配项。PCRE 将返回您寻找的结果,^|\w
因为它似乎返回位置零长度匹配以及字符匹配。
工作正则表达式
您可以使用以下模式来匹配/捕获您正在寻找的结果:
请参阅此处使用的正则表达式(查看右侧的匹配信息)
^(?=(\w)|)|\w
工作代码
下面的代码演示了如何将上述正则表达式的结果转换为数组(exec
在循环中使用,然后过滤结果以确保我们在捕获组为 时删除它undefined
)。
const regex = /^(?=(\w)|)|\w/g;
const str = `ab`;
let matches=[];
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
matches = matches.concat(m.filter(function(el){return typeof el !== 'undefined'}))
}
console.log(matches)
或使用matchAll
如下评论中所述:
const regex = /^(?=(\w)|)|\w/g;
const str = `ab`;
let matches=[];
for (const a of str.matchAll(regex) {
matches = matches.concat(a.filter(function(el){return typeof el !== 'undefined'}))
}
console.log(matches)
甚至更短:
x = [...'ab'.matchAll(/^(?=(\w)|)|\w/g)].flat().filter(n=>n!==undefined)
console.log(x)
推荐阅读
- amazon-web-services - 如何在 boto3 中的 SQS FIFO 队列上配置 Lambda 触发器?
- javascript - Axios在react js中获取请求时出错
- ios - com.esri.arcgis.runtime.error Code=10018 错误的词法转换
- reactjs - 在 React 中的组件之间传递状态
- javascript - React.js:更新嵌套对象的状态
- c++ - 如何将多维映射从 c++ 转换为 perl 中的哈希
- java - 如何将参数传递给 Jackson @JsonSerialize 构造函数?
- instagram-api - 从 Instagram API 获取公共 Instagram 帐户(非业务)的洞察媒体数据(如评论和位置)
- python-3.x - 如何让顶部框架覆盖整个水平空间?
- sql - 在 Redshift SQL 中使用 ID 和日期范围删除事务