JS RegEx即使它应该[重复]也不匹配
这个问题在这里已有答案:
- Why does a RegExp with global flag give wrong results? 6个答案
你的正则表达式使用g
标志,这意味着它们保持状态。例如,你已经说过你的第二个字符串与你的任何一个表达式都不匹配,但是如果表达式从头开始:
const rex = /(?<date>\d{4}-\d{2}-\d{2}) (?<time>\d{2}_\d{2}_\d{2})(-| - )(?<window>.*?)(?<extension>\.(png|jpg|jpeg))/g;
const str = "2017-08-31 18_57_43-shouldwork.png";
console.log(rex.exec(str)); // Works
console.log(rex.exec(str)); // Fails
.as-console-wrapper {
max-height: 100% !important;
}
JS RegEx即使它应该[重复]也不匹配
这个问题在这里已有答案:
- Why does a RegExp with global flag give wrong results? 6个答案
你的正则表达式使用g
标志,这意味着它们保持状态。例如,你已经说过你的第二个字符串与你的任何一个表达式都不匹配,但是如果表达式从头开始:
const rex = /(?<date>\d{4}-\d{2}-\d{2}) (?<time>\d{2}_\d{2}_\d{2})(-| - )(?<window>.*?)(?<extension>\.(png|jpg|jpeg))/g;
const str = "2017-08-31 18_57_43-shouldwork.png";
console.log(rex.exec(str)); // Works
console.log(rex.exec(str)); // Fails
.as-console-wrapper {
max-height: 100% !important;
}
与本文相关的文章
评论列表 (0)
- 暂无评论