MCPcopy
hub / github.com/philc/vimium / nthRegexIndex

Function nthRegexIndex

background_scripts/commands.js:44–61  ·  view source on GitHub ↗
(str, regex, n)

Source from the content-addressed store, hash-verified

42
43// Returns the index of the nth occurrence of the regexp in the string. -1 if not found.
44function 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
63const KeyMappingsParser = {
64 // Parses the text supplied by the user in their "keyMappings" setting.

Callers 1

parseFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected