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.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
特性属性在
RegExp.prototype.sticky
|
|
|---|---|
| 可写 | no |
| 可枚举 | no |
| 可配置 | yes |
值
sticky
是
布尔
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
标志。
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 (if
multiline
is
true
) 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
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'RegExp.prototype.sticky' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
sticky
|
Chrome 49 | Edge 13 | Firefox 3 | IE No | Opera 36 | Safari 10 | WebView Android 49 | Chrome Android 49 | Firefox Android 4 | Opera Android 36 | Safari iOS 10 | Samsung Internet Android 5.0 | nodejs Yes |
| Anchored sticky flag behavior per ES2015 | Chrome 49 | Edge 13 | Firefox 44 | IE No | Opera 36 | Safari 10 | WebView Android 49 | Chrome Android 49 | Firefox Android 44 | Opera Android 36 | Safari iOS 10 | Samsung Internet Android 5.0 | nodejs ? |
| Prototype accessor property (ES2015) | Chrome 49 | Edge 13 | Firefox 38 | IE No | Opera 36 | Safari 10 | WebView Android 49 | Chrome Android 49 | Firefox Android 38 | Opera Android 36 | Safari iOS 10 | Samsung Internet Android 5.0 | nodejs Yes |
完整支持
不支持
兼容性未知
RegExp.lastIndex
RegExp.prototype.global
RegExp.prototype.ignoreCase
RegExp.prototype.multiline
RegExp.prototype.source
RegExp
RegExp.$1-$9
RegExp.input ($_)
RegExp.lastMatch ($&)
RegExp.lastParen ($+)
RegExp.leftContext ($`)
RegExp.prototype.dotAll
RegExp.prototype.flags
RegExp.prototype.global
RegExp.prototype.ignoreCase
RegExp.prototype.multiline
RegExp.prototype.source
RegExp.prototype.sticky
RegExp.prototype.unicode
RegExp.rightContext ($')
RegExpInstance.lastIndex
get RegExp[@@species]
Function
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()