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 |
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). A regular expression defined as both sticky
and global
ignores the global
flag.
You cannot change this property directly. It is read-only.
var str = '#foo#'; var regex = /foo/y; regex.lastIndex = 1; regex.test(str); // true regex.lastIndex = 5; regex.test(str); // false (lastIndex is taken into account with sticky flag) regex.lastIndex; // 0 (reset after match failure)
For several versions, Firefox's SpiderMonkey engine had a bug with regard to the ^
assertion and the sticky flag which allowed expressions starting with the ^
assertion and using the sticky flag to match when they shouldn't. The bug was introduced some time after Firefox 3.6 (which had the sticky flag but not the bug) and fixed in 2015. Perhaps because of the bug, the ES2015 specification specifically calls out the fact that:
When the
y
flag is used with a pattern, ^ always matches only at the beginning of the input, or (ifmultiline
istrue
) at the beginning of a line.
Examples of correct behavior:
var regex = /^foo/y; regex.lastIndex = 2; regex.test('..foo'); // false - index 2 is not the beginning of the string var regex2 = /^foo/my; regex2.lastIndex = 2; regex2.test('..foo'); // false - index 2 is not the beginning of the string or line regex2.lastIndex = 2; regex2.test('.\nfoo'); // true - index 2 is the beginning of a line
Specification | Status | Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'RegExp.prototype.sticky' in that specification. | Standard | Initial definition. |
ECMAScript 2017 Draft (ECMA-262) The definition of 'RegExp.prototype.sticky' in that specification. | Draft |
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | 49 | 13 | 3.0 (1.9) | No support | 36 | 10 |
Prototype accessor property | ? | (Yes) | 38 (38) | No support | ? | ? |
Anchored sticky(y) flag behavior per ES2015 | ? | (Yes) | 44 (44) | No support | ? | ? |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | No support | 49 | 1.0 (1.9) | No support | (Yes) | 10 |
Prototype accessor property | No support | ? | 38.0 (38) | No support | ? | ? |
Anchored sticky(y) flag behavior per ES2015 | No support | ? | 44.0 (44) | No support | ? | ? |
RegExp.lastIndex
RegExp.prototype.global
RegExp.prototype.ignoreCase
RegExp.prototype.multiline
RegExp.prototype.source
© 2005–2017 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/sticky