Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 530055 of regexp.lastIndex

  • Revision slug: Web/JavaScript/Reference/Global_Objects/RegExp/lastIndex
  • Revision title: RegExp.lastIndex
  • Revision id: 530055
  • Created:
  • Creator: aaco
  • Is current revision? No
  • Comment

Revision Content

{{JSRef("Global_Objects", "RegExp")}}

Summary

The lastIndex is a read/write integer property of regular expressions that specifies the index at which to start the next match.

{{js_property_attributes(1,0,0)}}

Syntax

lastIndex = regExpObj.lastIndex;

Description

This property is set only if the regular expression used the "g" flag to indicate a global search. The following rules apply:

  • If lastIndex is greater than the length of the string, regexp.test and regexp.exec fail, then lastIndex is set to 0.
  • If lastIndex is equal to the length of the string and if the regular expression matches the empty string, then the regular expression matches input starting at lastIndex.
  • If lastIndex is equal to the length of the string and if the regular expression does not match the empty string, then the regular expression mismatches input, and lastIndex is reset to 0.
  • Otherwise, lastIndex is set to the next position following the most recent match.

Examples

Consider the following sequence of statements:

var re = /(hi)?/g;

Matches the empty string.

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

Returns \"hi\" with lastIndex equal to 2.

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

Returns [\"\"], an empty array whose zeroth element is the match string. In this case, the empty string because lastIndex was 2 (and still is 2) and "hi" has length 2.

Specifications

Specification Status Comment
ECMAScript 3rd Edition. Implemented in JavaScript 1.2. Standard Initial definition.
JavaScript 1.5: lastIndex is a property of a RegExp instance, not the RegExp object.
{{SpecName('ES5.1', '#sec-15.10.7.5', 'RegExp.lastIndex')}} {{Spec2('ES5.1')}}  
{{SpecName('ES6', '#sec-properties-of-regexp-instances', 'RegExp.lastIndex')}} {{Spec2('ES6')}}  

Browser compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }}

See also

  • {{jsxref("RegExp.prototype.ignoreCase")}}
  • {{jsxref("RegExp.prototype.global")}}
  • {{jsxref("RegExp.prototype.multiline")}}
  • {{jsxref("RegExp.prototype.source")}}
  • {{jsxref("RegExp.prototype.sticky")}}

Revision Source

<div>
 {{JSRef("Global_Objects", "RegExp")}}</div>
<h2 id="Summary" name="Summary">Summary</h2>
<p>The <code><strong>lastIndex</strong></code> is a read/write integer property of regular expressions that specifies the index at which to start the next match.</p>
<div>
 {{js_property_attributes(1,0,0)}}</div>
<h2 id="Syntax">Syntax</h2>
<pre class="syntaxbox">
<var>lastIndex</var> = <var>regExpObj</var>.lastIndex;
</pre>
<h2 id="Description" name="Description">Description</h2>
<p>This property is set only if the regular expression used the "<code>g</code>" flag to indicate a global search. The following rules apply:</p>
<ul>
 <li>If <code>lastIndex</code> is greater than the length of the string, <code>regexp.test</code> and <code>regexp.exec</code> fail, then&nbsp;<code>lastIndex</code> is set to 0.</li>
 <li>If <code>lastIndex</code> is equal to the length of the string and if the regular expression matches the empty string, then the regular expression matches input starting at <code>lastIndex</code>.</li>
 <li>If <code>lastIndex</code> is equal to the length of the string and if the regular expression does not match the empty string, then the regular expression mismatches input, and <code>lastIndex</code> is reset to 0.</li>
 <li>Otherwise, <code>lastIndex</code> is set to the next position following the most recent match.</li>
</ul>
<h2 id="Examples">Examples</h2>
<p>Consider the following sequence of statements:</p>
<pre class="brush:js">
var re = /(hi)?/g;
</pre>
<p>Matches the empty string.</p>
<pre class="brush:js">
console.log(re.exec("hi"));
console.log(re.lastIndex);</pre>
<p>Returns <code>\"hi\"</code> with <code>lastIndex</code> equal to 2.</p>
<pre class="brush:js">
console.log(re.exec("hi"));
console.log(re.lastIndex);
</pre>
<p>Returns <code>[\"\"]</code>, an empty array whose zeroth element is the match string. In this case, the empty string because <code>lastIndex</code> was 2 (and still is 2) and "<code>hi</code>" has length 2.</p>
<h2 id="Specifications">Specifications</h2>
<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>ECMAScript 3rd Edition. Implemented in JavaScript 1.2.</td>
   <td>Standard</td>
   <td>Initial definition.<br />
    JavaScript 1.5: <code>lastIndex</code> is a property of a <code>RegExp</code> instance, not the <code>RegExp</code> object.</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-15.10.7.5', 'RegExp.lastIndex')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-properties-of-regexp-instances', 'RegExp.lastIndex')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{ CompatibilityTable() }}</p>
<div id="compat-desktop">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Chrome</th>
    <th>Firefox (Gecko)</th>
    <th>Internet Explorer</th>
    <th>Opera</th>
    <th>Safari</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
   </tr>
  </tbody>
 </table>
</div>
<div id="compat-mobile">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Android</th>
    <th>Chrome for Android</th>
    <th>Firefox Mobile (Gecko)</th>
    <th>IE Mobile</th>
    <th>Opera Mobile</th>
    <th>Safari Mobile</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
   </tr>
  </tbody>
 </table>
</div>
<h2 id="See_also" name="See_also">See also</h2>
<ul>
 <li>{{jsxref("RegExp.prototype.ignoreCase")}}</li>
 <li>{{jsxref("RegExp.prototype.global")}}</li>
 <li>{{jsxref("RegExp.prototype.multiline")}}</li>
 <li>{{jsxref("RegExp.prototype.source")}}</li>
 <li>{{jsxref("RegExp.prototype.sticky")}}</li>
</ul>
Revert to this revision