My problem
I've been confused by the following code snippet. The strings seems identical regex-wise (the only difference is a digit that should be matched with \d
. In essence, the first string is matched while the second does not.
After playing around with it, it became clear that the order matters: only the first string is matched.
const regex = /departure\stime\s+([\d:]+)[\s\S]*arrival\stime\s+([\d:]+)[\s\S]*Platform\s+(\S+)[\s\S]*Duration\s([\d:]+)/gm;
const s1 = '\n departure time 05:42\n \n arrival time 06:39\n Boarding the train from Platform 3\n \n Switch train in \n No changing\n \n Change\n \n \n \n \n Access for handicapped.\n reserved seats\n \n \n Duration 00:57\n \n ';
const s2 = '\n departure time 05:12\n \n arrival time 06:09\n Boarding the train from Platform 3\n \n Switch train in \n No changing\n \n Change\n \n \n \n \n Access for handicapped.\n reserved seats\n \n \n Duration 00:57\n \n ';
console.log('Match: ', regex.exec(s1));
console.log('No Match:', regex.exec(s2));
My question
How can I use the same regex to match multiple strings, without worrying that the previous match might alter the match?