You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: files/en-us/web/javascript/reference/global_objects/regexp/sticky/index.md
+31-5Lines changed: 31 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -20,14 +20,40 @@ The **`sticky`** accessor property indicates whether or not the `y` flag is used
20
20
21
21
## Description
22
22
23
-
`RegExp.prototype.sticky` has the value `true` if the `y` flag was used; otherwise, `false`. The `y` flag indicates that it matches only from the index indicated by the {{jsxref("RegExp.lastIndex", "lastIndex")}} property of this regular expression in the target string (and does not attempt to match from any later indexes).
24
-
25
-
When the [`exec()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec) method is called and the regex fails to match at the current position indicated by {{jsxref("RegExp.lastIndex", "lastIndex")}}, a global regex will continue to attempt at the next character, while a sticky regex immediately returns `null` and resets `lastIndex` to 0. When the match succeeds, `lastIndex` is advanced to the end of the match, just like global regexps do. When `lastIndex` is out of bounds of the currently matched string, it's reset to 0, just like global regexps do.
26
-
27
-
For the [`exec()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec) method, a regex that's both sticky and [global](/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/global) behaves the same as a sticky and non-global regex. However, due to many other methods special-casing the behavior of global regexps, the global flag is, in general, orthogonal to the sticky flag. For more information about how each individual regex-related method (such as [`String.prototype.match()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match) and [`String.prototype.replace()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace)) interact with the sticky and global flags, see their respective pages.
23
+
`RegExp.prototype.sticky` has the value `true` if the `y` flag was used; otherwise, `false`. The `y` flag indicates that the regex attempts to match the target string only from the index indicated by the {{jsxref("RegExp.lastIndex", "lastIndex")}} property (and unlike a global regex, does not attempt to match from any later indexes).
28
24
29
25
The set accessor of `sticky` is `undefined`. You cannot change this property directly.
30
26
27
+
For both sticky regexes and [global](/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/global) regexes:
28
+
29
+
- They start matching at `lastIndex`.
30
+
- When the match succeeds, `lastIndex` is advanced to the end of the match.
31
+
- When `lastIndex` is out of bounds of the currently matched string, `lastIndex` is reset to 0.
32
+
33
+
However, for the [`exec()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec) method, the behavior when matching fails is different:
34
+
35
+
- When the [`exec()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec) method is called on a sticky regex, if the regex fails to match at `lastIndex`, the regex immediately returns `null` and resets `lastIndex` to 0.
36
+
- When the [`exec()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec) method is called on a global regex, if the regex fails to match at `lastIndex`, it tries to match from the next character, and so on until a match is found or the end of the string is reached.
37
+
38
+
For the [`exec()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec) method, a regex that's both sticky and global behaves the same as a sticky and non-global regex. Because [`test()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test) is a simple wrapper around `exec()`, `test()` would ignore the global flag and perform sticky matches as well. However, due to many other methods special-casing the behavior of global regexes, the global flag is, in general, orthogonal to the sticky flag.
39
+
40
+
-[`String.prototype.matchAll()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/matchAll) (which calls [`RegExp.prototype[@@matchAll]()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/@@matchAll)): `y`, `g` and `gy` are all different.
41
+
- For `y` regexes: `matchAll()` throws; [`[@@matchAll]()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/@@matchAll) yields the `exec()` result exactly once, without updating the regex's `lastIndex`.
42
+
- For `g` or `gy` regexes: returns an iterator that yields a sequence of `exec()` results.
43
+
-[`String.prototype.match()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match) (which calls [`RegExp.prototype[@@match]()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/@@match)): `y`, `g` and `gy` are all different.
44
+
- For `y` regexes: returns the `exec()` result and updates the regex's `lastIndex`.
45
+
- For `g` or `gy` regexes: returns an array of all `exec()` results.
46
+
-[`String.prototype.search()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/search) (which calls [`RegExp.prototype[@@search]()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/@@search)): the `g` flag is always irrelevant.
47
+
- For `y` or `gy` regexes: always returns `0` (if the very beginning of the string matches) or `-1` (if the beginning doesn't match), without updating the regex's `lastIndex` when it exits.
48
+
- For `g` regexes: returns the index of the first match in the string, or `-1` if no match is found.
49
+
-[`String.prototype.split()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split) (which calls [`RegExp.prototype[@@split]()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/@@split)): `y`, `g`, and `gy` all have the same behavior.
50
+
-[`String.prototype.replace()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) (which calls [`RegExp.prototype[@@replace]()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/@@replace)): `y`, `g` and `gy` are all different.
51
+
- For `y` regexes: replaces once at the current `lastIndex` and updates `lastIndex`.
52
+
- For `g` and `gy` regexes: replaces all occurrences matched by `exec()`.
53
+
-[`String.prototype.replaceAll()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) (which calls [`RegExp.prototype[@@replace]()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/@@replace)): `y`, `g` and `gy` are all different.
54
+
- For `y` regexes: `replaceAll()` throws.
55
+
- For `g` and `gy` regexes: replaces all occurrences matched by `exec()`.
56
+
31
57
## Examples
32
58
33
59
### Using a regular expression with the sticky flag
Copy file name to clipboardExpand all lines: files/en-us/web/javascript/reference/global_objects/regexp/unicode/index.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -23,7 +23,7 @@ The **`unicode`** accessor property indicates whether or not the `u` flag is use
23
23
24
24
- Any [Unicode code point escapes](/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Unicode_Property_Escapes) (`\u{xxxx}`, `\p{UnicodePropertyValue}`) will be interpreted as such instead of as literal characters.
25
25
- Surrogate pairs will be interpreted as whole characters instead of two separate characters. For example, `/[😄]/u` would only match `"😄"` but not `"\ud83d"`.
26
-
- When [`lastIndex`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastIndex) is automatically advanced (such as when calling [`exec()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec)), unicode regexps advance by Unicode code points instead of UTF-16 code units.
26
+
- When [`lastIndex`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastIndex) is automatically advanced (such as when calling [`exec()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec)), unicode regexes advance by Unicode code points instead of UTF-16 code units.
27
27
28
28
There are other changes to the parsing behavior that prevent possible syntax mistakes (which are analogous to [strict mode](/en-US/docs/Web/JavaScript/Reference/Strict_mode) for regex syntax). This is explained in more detail in [Using Unicode regular expressions](/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#using_unicode_regular_expressions).
0 commit comments