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 984283 of regexp.lastIndex

  • Revision slug: Web/JavaScript/Reference/Global_Objects/RegExp/lastIndex
  • Revision title: RegExp.lastIndex
  • Revision id: 984283
  • Created:
  • Creator: fscholz
  • Is current revision? No
  • Comment add ES draft

Revision Content

{{JSRef}}

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

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, {{jsxref("RegExp.prototype.test()", "test()")}} and {{jsxref("RegExp.prototype.exec()", "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

Using lastIndex

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", "hi"] with lastIndex equal to 2.

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

Returns ["", undefined], 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
{{SpecName('ES3')}} {{Spec2('ES3')}} Initial definition. Implemented in JavaScript 1.2. JavaScript 1.5: lastIndex is a property of a {{jsxref("RegExp")}} instance, not the {{jsxref("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')}}  
{{SpecName('ESDraft', '#sec-properties-of-regexp-instances', 'RegExp.lastIndex')}} {{Spec2('ESDraft')}}  

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}}</div>

<p>The <strong><code>lastIndex</code></strong> 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">
<code><var>regExpObj</var>.lastIndex</code></pre>

<h2 id="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, {{jsxref("RegExp.prototype.test()", "test()")}} and {{jsxref("RegExp.prototype.exec()", "exec()")}} fail, then <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>

<h3 id="Using_lastIndex">Using <code>lastIndex</code></h3>

<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", "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>["", undefined]</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>{{SpecName('ES3')}}</td>
   <td>{{Spec2('ES3')}}</td>
   <td>Initial definition. Implemented in JavaScript 1.2. JavaScript 1.5: <code>lastIndex</code> is a property of a {{jsxref("RegExp")}} instance, not the {{jsxref("RegExp")}} 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>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-properties-of-regexp-instances', 'RegExp.lastIndex')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<div>{{CompatibilityTable}}</div>

<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">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