mozilla

RegExp.prototype.sticky

This is a new technology, part of the ECMAScript 2015 (ES6) standard .
This technology's specification has been finalized, but check the compatibility table for usage and implementation status in various browsers.

The sticky property reflects whether or not the search is sticky (searches in strings only from the index indicated by the lastIndex property of this regular expression). sticky is a read-only property of an individual regular expression object.

Property attributes of RegExp.prototype.sticky
Writable no
Enumerable no
Configurable yes

Description

The value of sticky is a Boolean and true if the "y" flag was used; otherwise, false. The "y" flag indicates that it matches only from the index indicated by the lastIndex property of this regular expression in the target string (and does not attempt to match from any later indexes). This allows the match-only-at-start capabilities of the character "^" to effectively be used at any location in a string by changing the value of the lastIndex property.

You cannot change this property directly.

Examples

Using a regular expression with the "sticky" flag

This example demonstrates how one could use the sticky flag on regular expressions to match individual lines of multiline input.

var text = 'First line\nsecond line';
var regex = /(\S+) line\n?/y;

var match = regex.exec(text);
console.log(match[1]);        // logs 'First'
console.log(regex.lastIndex); // logs '11'

var match2 = regex.exec(text);
console.log(match2[1]);       // logs 'Second'
console.log(regex.lastIndex); // logs '22'

var match3 = regex.exec(text);
console.log(match3 === null); // logs 'true'

Compatibility test

One can test at runtime whether the sticky flag is supported, using try { … } catch { … }. For this, either an eval(…) expression or the RegExp(regex-stringflags-string) syntax must be used (since the /regex/flags notation is processed at compile-time, so it throws an exception before the catch block is encountered). For example:

var supports_sticky;
try { RegExp('', 'y'); supports_sticky = true; }
catch(e) { supports_sticky = false; }
console.log(supports_sticky); // logs 'false' in Firefox 2, 
                              //        'true'  in Firefox 3+

Specifications

Specification Status Comment
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'RegExp.prototype.sticky' in that specification.
Standard Initial definition.

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support Not supported 3.0 (1.9) Not supported Not supported Not supported
Prototype accessor property Not supported 38 (38) Not supported Not supported Not supported
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support Not supported Not supported 1.0 (1.9) Not supported Not supported Not supported
Prototype accessor property Not supported Not supported 38.0 (38) Not supported Not supported Not supported

See also

Document Tags and Contributors

Contributors to this page: Sheppy, fscholz, ziyunfei, ostorj, kscarfone, Mingun, stevemao
Last updated by: fscholz,