MDN may have intermittent access issues April 18 13:00 - April 19 01:00 UTC. See whistlepig.mozilla.org for all notifications.

mozilla
Your Search Results

    RegExp.prototype.sticky

    This is an experimental technology, part of the ECMAScript 6 (Harmony) proposal.
    Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future version of browsers as the spec changes.

    Summary

    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

    Example: 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]);        // prints 'First'
    console.log(regex.lastIndex); // prints '11'
    
    var match2 = regex.exec(text);
    console.log(match2[1]);       // prints 'Second'
    console.log(regex.lastIndex); // prints '22'
    
    var match3 = regex.exec(text);
    console.log(match3 === null); // prints '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); // prints 'false' in Firefox 2, 
                                  //        'true'  in Firefox 3+
    

    Specifications

    Specification Status Comment
    ECMAScript 6 (ECMA-262)
    The definition of 'RegExp.prototype.sticky' in that specification.
    Release Candidate 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, stevemao, ziyunfei, kscarfone, ostorj, Mingun
    Last updated by: fscholz,