Notice:
The WebPlatform project has been discontinued when the Stewards partnership agreement ended in September 2015. This site has been frozen, and the assets we have created will remain.

sticky

Jump to: navigation, search

sticky

This article is Ready to Use.

Summary

Returns a Boolean value indicating the state of the sticky flag ( y ) used with a regular expression. Default is false. Read-only.


Syntax

JavaScript

regex.sticky




Examples

The following example illustrates the use of the sticky property.

JavaScript

var regex = /foo.bar/gy;
regex.sticky;
// → true
regex.lastIndex;
// → 0

regex.test('foo*bar');
// → true
regex.lastIndex;
// → 7
regex.test('..foo*bar');
// → false

regex.lastIndex = 0;
regex.test('..foo*bar');
// → false

regex.lastIndex = 2;
regex.test('..foo*bar');
// → true
regex.lastIndex;
// → 9

Remarks

The sticky property returns true if the sticky flag is set for a regular expression, and returns false if it is not.

The sticky flag, when used, indicates that the regular expression performs sticky matching in the target string by attempting to match starting at lastIndex. If matching at that location fails, then null is returned, i.e., no forward “anchoring” search is performed. If matching succeeds, then the regular expression’s lastIndex property is updated as for the flag g.

See also

Other articles