1

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?

5

1 Answer 1

1

When you use the 'g' flag in your regex, .exec() will return an index into the lastIndex property on the regex object. Then, when you attempt to use .exec() again with the same regex, it will begin the search at the index specified in lastIndex.

There are a few ways to overcome this:

1) Remove the 'g' flag. lastIndex will stay set at 0
2) Use .match(), .test(), or .search()
3) Manually reset the lastIndext after each call to .exec()
    // for example:
    let results = regex.exec(s1);
    regex.lastIndex = 0;

See the docs here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.