| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastIndex | [Back] [Original] |
Get to know MDN better
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
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
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"']
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")); // falselastIndex
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"
| ECMAScript 2027 LanguageSpecification # sec-properties-of-regexp-instances |
| Web Proxy Viewer | New URL | Original Page |