chAll) (which calls [ ` RegExp.prototype[@@matchAll]() ` ](/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/@@matchAll) ): ` y ` , ` g ` and ` gy ` are all different. 
    * For ` y ` regexes: ` matchAll() ` throws; ` [@@matchAll]() ` yields the ` exec() ` result exactly once, without updating the regex's ` lastIndex ` . 
    * For ` g ` or ` gy ` regexes: returns an iterator that yields a sequence of ` exec() ` results. 
  * [ ` 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. 
    * For ` y ` regexes: returns the ` exec() ` result and updates the regex's ` lastIndex ` . 
    * For ` g ` or ` gy ` regexes: returns an array of all ` exec() ` results. 
  * [ ` 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. 
    * 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. 
    * For ` g ` regexes: returns the index of the first match in the string, or ` -1 ` if no match is found. 
  * [ ` 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. 
  * [ ` 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. 
    * For ` y ` regexes: replaces once at the current ` lastIndex ` and updates ` lastIndex ` . 
    * For ` g ` and ` gy ` regexes: replaces all occurrences matched by ` exec() ` . 
  * [ ` String.prototype.replaceAll() ` ](/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll) (which calls [ ` RegExp.prototype[@@replace]() ` ](/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/@@replace) ): ` y ` , ` g ` and ` gy ` are all different. 
    * For ` y ` regexes: ` replaceAll() ` throws. 
    * For ` g ` and ` gy ` regexes: replaces all occurrences matched by ` exec() ` . 

##  Examples

###  Using a regular expression with the sticky flag

js

    
    
    const str = "#foo#";
    const 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)
    

###  Anchored sticky flag

For several versions, Firefox's SpiderMonkey engine had [ a bug
](https://bugzil.la/773687) 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 specification [ specifically calls out
](https://tc39.es/ecma262/multipage/text-processing.html#sec-compileassertion)
the fact that:

> Even when the ` y ` flag is used with a pattern, ` ^ ` always matches only
> at the beginning of _Input_ , or (if _rer_ .[[Multiline]] is ` true ` ) at
> the beginning of a line.

Examples of correct behavior:

js

    
    
    const regex = /^foo/y;
    regex.lastIndex = 2;
    regex.test("..foo"); // false - index 2 is not the beginning of the string
    
    const regex2 = /^foo/my;
    regex2.lastIndex = 2;
    regex2.test("..foo"); // false -