Regex matches condition words

I have a line 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 the line above, for each line, I would like to match tags matching ATEAM- [0-9] * before the #finish tag. If any line does not have # finish, all ATEAM tags should be ignored.

Here is an irregular 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 a Regex solution, and this is what I tried

 msg.match(/(ATEAM-[0-9]*|\#finish)/g); 

But this gives me the result, as in this fiddle , but its not as expected. Can someone help me with this?

+4
source share
1 answer
 ATEAM-[0-9]+(?=.*?#finish) 

You can do this using lookahead Watch the demo.

https://regex101.com/r/uF4oY4/88

+4
source

All Articles