正则表达式中 \G 锚点的用途 [英] Purpose of the \G anchor in regular expressions

查看:30
本文介绍了正则表达式中 \G 锚点的用途的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是不明白 \G 锚点的作用.

I simply don't understand what the \G anchor does.

如果我在 1122aa33 上执行 /\G\d\d/,它将匹配 1122>.但是,当我在 1122aa33 上尝试 /\d\d\G/ 时,它什么都不匹配.

If I execute /\G\d\d/ on 1122aa33, it will match 11 and 22. However, when I try /\d\d\G/ on 1122aa33, it matches nothing.

有人能指教我吗?

推荐答案

\G 是一个 匹配前一个匹配位置的锚.

在第一遍时,\G 等价于 \A,它是字符串锚点的开始.由于 \d\d\A 永远不会匹配任何东西(因为你怎么能在字符串的开头之前有两个数字?),\d\d\G 也将从不匹配任何东西.

On the first pass, \G is equivalent to \A, which is the start of the string anchor. Since \d\d\A will never match anything (because how can you have two digits before the start of the string?), \d\d\G will also never match anything.

这篇关于正则表达式中 \G 锚点的用途的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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