How to match multiple strings with a single regex in nodejs?

问题内容:

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?

问题评论:

1  
Note that the behaviors of String.prototype.match() and RegExp.prototype.exec() are quite different with respect to what the g flag does. Why are you using the “g” flag at all? Have you tried without “g”?
1  
Either you need to manually reset lastIndex or you should be using match
    
    
Use String.prototype.match instead of RegExp.prototype.exec.
    

原文地址:

https://stackoverflow.com/questions/47756343/how-to-match-multiple-strings-with-a-single-regex-in-nodejs

添加评论