[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastIndex [Back]  [Original]

RegExp: lastIndex - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

RegExp: lastIndex

Baseline

20157

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

js
const re = /(hi)?/g;

js
console.log(re.exec("hi"));
console.log(re.lastIndex);

["hi", "hi"] lastIndex 2

js
console.log(re.exec("hi"));
console.log(re.lastIndex);

["", undefined] 0 lastIndex 2 ( 2) hi 2

lastIndex sticky

lastIndex

y lastIndex lastIndex

js
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

js
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

js
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;

js
stringPattern.lastIndex = offset;
const nextString = stringPattern.exec(remainingInput);
console.log(nextString[0]); // "world"
offset = stringPattern.lastIndex;

exec() exec()

js
const re = /foo/g;
console.log(re.test("foo bar")); // true
console.log(re.test("foo baz")); // falselastIndex 

lastIndex lastIndex

js
const re = /foo/g;
console.log(re.test("foo bar")); // true
re.lastIndex = 0;
console.log(re.test("foo baz")); // true

exec() lastIndex

js
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