[英]Regex to match words with condition
I have a string like this 我有这样的字符串
var msg = "ATEAM-3121 ATEAM-31 123 ATEAM-32 #finish \n ATEAM-3211 \n ATEAM-51 ATEAM-52 ATEAM-53 12345677 #finish ATEAM-1000";
In above string, for each line, I would like to match tags matching ATEAM-[0-9]* before #finish tag. 在上面的字符串中,对于每一行,我想在#finish标签之前匹配匹配ATEAM- [0-9] *的标签。 If any line does not have #finish, all ATEAM tags should be ignored.
如果任何行没有#finish,则应忽略所有ATEAM标记。
Here is the non regex solution that works 这是非正则表达式的解决方案
msg = msg.split("\n");
var tickets = [];
msg.forEach(function(val) {
var ticks = val.split(' ');
for(var i = ticks.indexOf('#finish'); i >= 0; i-- ) {
if(ticks[i].match(/^ATEAM-.*$/)) {
tickets.push(ticks[i]);
}
}
});
console.log(tickets);
But would like to convert it to Regex solution and this is what I have tried 但是想将它转换为Regex解决方案,这就是我所尝试的
msg.match(/(ATEAM-[0-9]*|\#finish)/g);
But it gives me result like in this fiddle , but its not as expected. 但它给我的结果就像这个小提琴 ,但它不像预期的那样。 Can anyone help me on this?
谁可以帮我这个事?
ATEAM-[0-9]+(?=.*?#finish)
You can do this using lookahead
.See demo. 你可以使用
lookahead
来做到这一点。参见演示。
https://regex101.com/r/uF4oY4/88 https://regex101.com/r/uF4oY4/88
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.