regex - 替换除第 3 次以外的所有匹配项
问题描述
我正在扫描二维码,需要一个脚本来用 (\t) 替换逗号
我的结果是:
820-20171-002,,2020 年 11 月 24 日,,,13,283.40,,Mike Shmow
我的问题是 - 我不想在日期之后使用逗号。现在我有以下内容 - 它可以用制表符替换逗号。
decodeResults[0].content.replace(/,/g, "\t");
我正在尝试用表达式替换 /,/g 以替换除第 3 次出现的所有逗号。
解决方案
利用
.replace(/(?<!\b[a-zA-Z]{3}\s+\d{1,2}(?=,\s*\d{4})),/g, '\t')
见证明
解释
--------------------------------------------------------------------------------
(?<! Negative lookbehind start, fail if pattern matches
--------------------------------------------------------------------------------
\b the boundary between a word char (\w)
and something that is not a word char
--------------------------------------------------------------------------------
[a-zA-Z]{3} any character of: 'a' to 'z', 'A' to 'Z'
(3 times)
--------------------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1
or more times (matching the most amount
possible))
--------------------------------------------------------------------------------
\d{1,2} digits (0-9) (between 1 and 2 times
(matching the most amount possible))
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
, ','
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ")
(0 or more times (matching the most
amount possible))
--------------------------------------------------------------------------------
\d{4} digits (0-9) (4 times)
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
) end of negative lookbehind
--------------------------------------------------------------------------------
, ','
推荐阅读
- python - 带有图像的 t-sne 的 python 实现
- mysql - MIN 和 MAX 值的 MySql 百分比
- r - 与我自己使用类库的代码相比,KNN 重复的 cv 方法返回不合理的结果
- asynchronous - perl6 "首先等待的操作"
- video-streaming - avconv 不能覆盖文件?
- amazon-web-services - 具有公共子网和私有子网的 AWS VPC - 实例可以 ping 自己,但不能互相 ping
- python - 在熊猫数据框中查找最大值的索引
- android - 多次注入同一个作用域对象实例
- css - 如何使用css制作带有狗骨轮廓的按钮?
- ios - 为什么每当玩家向左或向右时它只播放我的左侧动画?