| 42 | |
| 43 | // Returns the index of the nth occurrence of the regexp in the string. -1 if not found. |
| 44 | function nthRegexIndex(str, regex, n) { |
| 45 | if (!regex.global) { |
| 46 | regex = new RegExp(regex.source, regex.flags + "g"); |
| 47 | } |
| 48 | let match; |
| 49 | let count = 0; |
| 50 | while ((match = regex.exec(str)) !== null) { |
| 51 | count++; |
| 52 | if (count === n) { |
| 53 | return match.index; |
| 54 | } |
| 55 | // Prevent infinite loop for zero-length matches. |
| 56 | if (match.index === regex.lastIndex) { |
| 57 | regex.lastIndex++; |
| 58 | } |
| 59 | } |
| 60 | return -1; |
| 61 | } |
| 62 | |
| 63 | const KeyMappingsParser = { |
| 64 | // Parses the text supplied by the user in their "keyMappings" setting. |