RegExp: lastIndex
Baseline
Widely available
This feature is well established and works across many devices and browser versions. Itâs been available across browsers since 2015å¹´7æ.
lastIndex 㯠RegExp ã¤ã³ã¹ã¿ã³ã¹ã®ãã¼ã¿ããããã£ã§ã次ã®ç
§åãéå§ããä½ç½®ãæå®ãã¾ãã
試ãã¦ã¿ã¾ããã
const regex = /foo/g;
const str = "table football, foosball";
regex.test(str);
console.log(regex.lastIndex);
// äºæ³ãããçµæ: 9
regex.test(str);
console.log(regex.lastIndex);
// äºæ³ãããçµæ: 19
å¤
éè² ã®æ´æ°ã§ãã
RegExp: lastIndex ã®ããããã£å±æ§ | |
|---|---|
| æ¸è¾¼å¯è½ | å¯ |
| åæå¯è½ | ä¸å¯ |
| è¨å®å¯è½ | ä¸å¯ |
解説
ãã®ããããã£ã¯ãæ£è¦è¡¨ç¾ã¤ã³ã¹ã¿ã³ã¹ãã°ãã¼ãã«æ¤ç´¢ã示ãããã« g ãã©ã°ã使ç¨ããå ´åãã¾ãã¯ç²ççæ¤ç´¢ã示ãããã« y ãã©ã°ã使ç¨ããå ´åã«ã®ã¿è¨å®ããã¾ããexec() ãæå®ãããå
¥åã«å¯¾ãã¦å¼ã³åºãããã¨ãã以ä¸ã®è¦åãé©ç¨ããã¾ãã
lastIndexãå ¥åã®é·ãããã大ããå ´åãexec()ã¯ä¸è´ãããã®ãè¦ã¤ãããããlastIndex㯠0 ã«è¨å®ããã¾ããlastIndexãå ¥åã®é·ã以ä¸ã§ãã£ãå ´åãexec()ã¯lastIndexããä¸è´ãããã®ãæ¢ããã¨ãã¾ããexec()ãä¸è´ãããã®ãè¦ã¤ããå ´ålastIndexã¯å ¥åã®ä¸ã®ä¸è´ããæååã®æ«å°¾ã®ä½ç½®ã«è¨å®ããã¾ããexec()ãä¸è´ãããã®ãè¦ã¤ããããªãã£ãå ´åãlastIndex㯠0 ã«è¨å®ããã¾ãã
ä»ã®æ£è¦è¡¨ç¾é¢é£ã®ã¡ã½ãããRegExp.prototype.test()ãString.prototype.match()ãString.prototype.replace() ãªã©ã¯ exec() ãå¼ã³åºãããããã lastIndex ã«å½±é¿ãåã¼ãã¾ãã詳細ã¯ããããã®ãã¼ã¸ãåç
§ãã¦ãã ããã
ä¾
>lastIndex ã®ä½¿ç¨
ä¾ãã°ã以ä¸ã®ä¸é£ã®å¦çãèãã¦ã¿ã¦ãã ããã
const re = /(hi)?/g;
空æååã«ä¸è´ãã¾ãã
console.log(re.exec("hi"));
console.log(re.lastIndex);
["hi", "hi"] ãè¿ãã lastIndex 㯠2 ã«ãªãã¾ãã
console.log(re.exec("hi"));
console.log(re.lastIndex);
è¿å¤ã¯ ["", undefined] ã§ã 0 çªç®ã®è¦ç´ ãä¸è´æååã¨ãªã空ã®é
åã§ãããã®å ´åã lastIndex ã 2 (ç¾å¨ã 2) ã§ãããhi ã®é·ãã 2 ã§ãããããç©ºã®æååã¨ãªãã¾ãã
lastIndex ã sticky ã®æ£è¦è¡¨ç¾ã§ä½¿ç¨
lastIndex ããããã£ã¯æ¸ãè¾¼ã¿å¯è½ã§ãããããè¨å®ãããã¨ã§ãæ£è¦è¡¨ç¾ãæå®ããã¤ã³ããã¯ã¹ããæ¬¡ã®æ¤ç´¢ãéå§ããããã«ãªãã¾ãã
y ãã©ã°ã¯ã»ã¨ãã©ã®å ´å lastIndex ãè¨å®ããå¿
è¦ãããã¾ããããã¯å¸¸ã« lastIndex ã¡ããã©ã®ä½ç½®ã§ç
§åãããã以éã®ä½ç½®ã§ã¯ç
§åã試ã¿ã¾ãããããã¯é常ãã¼ãµã¼ãæ¸ãã¨ãã«ãç¾å¨ã®ä½ç½®ã®ãã¼ã¯ã³ã®ã¿ã«ä¸è´ããããå ´åã«ä¾¿å©ã§ãã
const stringPattern = /"[^"]*"/y;
const input = `const message = "Hello world";`;
stringPattern.lastIndex = 6;
console.log(stringPattern.exec(input)); // null
stringPattern.lastIndex = 16;
console.log(stringPattern.exec(input)); // ['"Hello world"']
lastIndex ã®å·»ãæ»ã
g ãã©ã°ã lastIndex ãè¨å®ãããã¨ã§æ©æµãåãããã¨ãã§ãã¾ãããããã使ç¨ä¾ã¨ãã¦ãã°ãã¼ãã«æ¤ç´¢ã®éä¸ã§æååã夿´ãããå ´åãããã¾ãããã®å ´åãæååãçããªãã¨ç¹å®ã®ä¸è´ãè¦éãã¦ãã¾ãå¯è½æ§ãããã¾ãããã®å ´å㯠lastIndex ãå·»ãæ»ããã¨ã§åé¿ã§ãã¾ãã
const mdLinkPattern = /\[[^[\]]+\]\((?<link>[^()\s]+)\)/dg;
function resolveMDLink(line) {
let match;
let modifiedLine = line;
while ((match = mdLinkPattern.exec(modifiedLine))) {
const originalLink = match.groups.link;
const resolvedLink = originalLink.replaceAll(/^files|\/index\.md$/g, "");
modifiedLine =
modifiedLine.slice(0, match.indices.groups.link[0]) +
resolvedLink +
modifiedLine.slice(match.indices.groups.link[1]);
// Rewind the pattern to the end of the resolved link
mdLinkPattern.lastIndex += resolvedLink.length - originalLink.length;
}
return modifiedLine;
}
console.log(
resolveMDLink(
"[`lastIndex`](files/ja/web/javascript/reference/global_objects/regexp/lastindex/index.md)",
),
); // [`lastIndex`](/ja/web/javascript/reference/global_objects/regexp/lastindex)
console.log(
resolveMDLink(
"[`ServiceWorker`](files/ja/web/api/serviceworker/index.md) and [`SharedWorker`](files/ja/web/api/sharedworker/index.md)",
),
); // [`ServiceWorker`](/ja/web/api/serviceworker) and [`SharedWorker`](/ja/web/api/sharedworker)
mdLinkPattern.lastIndex += resolvedLink.length - originalLink.length ã®è¡ãåé¤ãã¦ã2 çªç®ã®ä¾ãå®è¡ãã¦ã¿ã¦ãã ãããæååãç縮ãããå¾ãlastIndex ãæ¢ã«ãªã³ã¯ã®ã¤ã³ããã¯ã¹ãéãã¦ããããã2 çªç®ã®ãªã³ã¯ãæ£ããç½®æãããªããã¨ããããã¾ãã
è¦å: ãã®ä¾ã¯ãã¢å°ç¨ã®ããã®ãã®ã§ããMarkdown ãæ±ãã«ã¯ãæ£è¦è¡¨ç¾ã®ä»£ããã«æ§æè§£æã©ã¤ãã©ãªã¼ã使ç¨ãã¦ãã ããã
æ¤ç´¢ã®æé©å
以åã®åºç¾ãç¡è¦ã§ãããã¤ã³ãã« lastIndex ãè¨å®ãããã¨ã§ãæ¤ç´¢ãæé©åãããã¨ãã§ãã¾ããä¾ãã°ããã®ä»£ããã«ã
const stringPattern = /"[^"]*"/g;
const input = `const message = "Hello " + "world";`;
// æååã®åã®é¨åã¯ãã§ã«å¦çãããã¨ã«ãã
let offset = 26;
const remainingInput = input.slice(offset);
const nextString = stringPattern.exec(remainingInput);
console.log(nextString[0]); // "world"
offset += nextString.index + nextString.length;
次ã®ããã«èãã¦ã¿ã¦ãã ããã
stringPattern.lastIndex = offset;
const nextString = stringPattern.exec(remainingInput);
console.log(nextString[0]); // "world"
offset = stringPattern.lastIndex;
ããã¯ãæååã®ã¹ã©ã¤ã¹ãé¿ããããã«ãªãã®ã§ãããæ§è½ãé«ã¾ãå¯è½æ§ãããã¾ãã
å¯ä½ç¨ã®åé¿
ããããã® exec() ã®å®è¡æ¯ã«å
¥åãç°ãªãå ´åãexec() ã«ãã£ã¦çºçããå¯ä½ç¨ãæ··ä¹±ãæãããã¾ããã
const re = /foo/g;
console.log(re.test("foo bar")); // true
console.log(re.test("foo baz")); // falseãlastIndex ãã¼ãã§ã¯ãªããã
ãã㯠lastIndex ãæã§å¤æ´ããå ´åã«ã¯ããã«æ··ä¹±ãã¾ããå¯ä½ç¨ãåããããã«ãåå
¥åãå®å
¨ã«å¦çãããå¾ã« lastIndex ããªã»ãããããã¨ãå¿ããªãã§ãã ããã
const re = /foo/g;
console.log(re.test("foo bar")); // true
re.lastIndex = 0;
console.log(re.test("foo baz")); // true
æ½è±¡åããã°ãexec() ãå¼ã³åºãåã« lastIndex ã«å
·ä½çãªå¤ãè¨å®ããããã«è¦æ±ãããã¨ãã§ãã¾ãã
function createMatcher(pattern) {
// ã³ãã¼ã使ããå
ã®æ£è¦è¡¨ç¾ãæ´æ°ãããªããããã
const regex = new RegExp(pattern, "g");
return (input, offset) => {
regex.lastIndex = offset;
return regex.exec(input);
};
}
const matchFoo = createMatcher(/foo/);
console.log(matchFoo("foo bar", 0)[0]); // "foo"
console.log(matchFoo("foo baz", 0)[0]); // "foo"
仿§æ¸
| Specification |
|---|
| ECMAScript® 2027 Language Specification> # sec-properties-of-regexp-instances> |