Skip to content

Sticky flag doesn't entirely mask the global flag #1454

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1 of 3 tasks
chrisdavidmills opened this issue Jan 18, 2021 · 6 comments · Fixed by #19540
Closed
1 of 3 tasks

Sticky flag doesn't entirely mask the global flag #1454

chrisdavidmills opened this issue Jan 18, 2021 · 6 comments · Fixed by #19540
Labels
Content:JS JavaScript docs effort: medium This task is a medium effort. help wanted If you know something about this topic, we would love your help!

Comments

@chrisdavidmills
Copy link
Contributor

@fireattack commented on Fri Nov 13 2020

Request type

  • Please close this issue, I accidentally submitted it without adding any details
  • New documentation
  • Correction or update

Details

It says

A regular expression defined as both sticky and global ignores the global flag.

But this doesn't seem to match my observation.

console.log('##-#'.replace(RegExp('#', 'gy'), 'x'));
console.log('##-#'.replace(RegExp('#', 'y'), 'x'));
console.log('##-#'.replace(RegExp('#', 'g'), 'x'));

The above all have different behaviors:

xx-#
x#-#
xx-x

(And they're as expected.)


@fireattack commented on Fri Nov 13 2020

Same error can be seen at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/global

Edit: a very detailed article about difference between y, g, yg: https://2ality.com/2020/01/regexp-lastindex.html

@Ryuno-Ki
Copy link
Collaborator

@rauschma It would be great, if you could lend us your expertise on this!

@rauschma
Copy link

/y and /yg only differ for the following methods:

  • .replace() replaces once vs. replaces multiple times
  • .replaceAll() throws if /g is missing
  • .match() works like .exec() vs. returns an Array with matches
  • .matchAll() throws if /g is missing

For .exec() and .test(), there is no difference.

See this table: https://gist.github.com/rauschma/8bd701d07f936c0c335681deaab612b5

@himanshugarg
Copy link
Contributor

Thanks @rauschma . @Ryuno-Ki any thoughts on how you'd like this to be handled given that the fix is requested on the pages for the flags but the behavior differs in the context of specific functions?

@Ryuno-Ki
Copy link
Collaborator

Ryuno-Ki commented Jun 3, 2021

I'm deferring to @yari-content-javascript (= @Rumyra @Elchi3 and @sideshowbarker) for an answer

@Rumyra
Copy link
Collaborator

Rumyra commented Jun 3, 2021

Seems to me that @rauschma 's info is worth mentioning on the specific method pages (if not present, I didn't check), as well as a relevant note on the pages for flags.

This can also be added to 'See also' sections https://2ality.com/2020/01/regexp-lastindex.html

@rauschma
Copy link

rauschma commented Jun 3, 2021

Thanks @Rumyra! This is a newer version of this content: https://exploringjs.com/impatient-js/ch_regexps.html#regexp-flags-gy

@Rumyra Rumyra added effort: medium This task is a medium effort. and removed 30 minute task labels Nov 29, 2021
@github-actions github-actions bot added the idle label Jan 5, 2022
@sideshowbarker sideshowbarker added the help wanted If you know something about this topic, we would love your help! label Jan 6, 2022
@teoli2003 teoli2003 reopened this May 29, 2022
@github-actions github-actions bot added the needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. label May 29, 2022
@sideshowbarker sideshowbarker removed the needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. label May 30, 2022
@Josh-Cena Josh-Cena changed the title https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/sticky Sticky flag doesn't entirely mask the global flag Jun 30, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 1, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Content:JS JavaScript docs effort: medium This task is a medium effort. help wanted If you know something about this topic, we would love your help!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants