Skip to content

Fix description of regex flags #19540

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

Merged
merged 7 commits into from
Aug 30, 2022
Merged

Fix description of regex flags #19540

merged 7 commits into from
Aug 30, 2022

Conversation

Josh-Cena
Copy link
Member

Summary

Motivation

Fix #1454 (finally!)
Fix #19524 — flags are changed to accessor properties in ES6.

Supporting details

Related issues

Metadata

  • Adds a new document
  • Rewrites (or significantly expands) a document
  • Fixes a typo, bug, or other error

@Josh-Cena Josh-Cena requested a review from a team as a code owner August 14, 2022 08:19
@Josh-Cena Josh-Cena requested review from wbamberg and removed request for a team August 14, 2022 08:19
@github-actions github-actions bot added the Content:JS JavaScript docs label Aug 14, 2022
@Josh-Cena Josh-Cena force-pushed the fix-regex-flag branch 3 times, most recently from a5f64ef to ab4433a Compare August 14, 2022 08:27
@github-actions
Copy link
Contributor

github-actions bot commented Aug 14, 2022

Preview URLs

Flaws

None! 🎉

External URLs

URL: /en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/global
Title: RegExp.prototype.global
on GitHub

No new external URLs


URL: /en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/unicode
Title: RegExp.prototype.unicode
on GitHub

No new external URLs


URL: /en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/source
Title: RegExp.prototype.source
on GitHub

No new external URLs


URL: /en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/flags
Title: RegExp.prototype.flags
on GitHub

No new external URLs


URL: /en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/sticky
Title: RegExp.prototype.sticky
on GitHub

No new external URLs


URL: /en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/hasIndices
Title: RegExp.prototype.hasIndices
on GitHub

No new external URLs


URL: /en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/dotAll
Title: RegExp.prototype.dotAll
on GitHub

No new external URLs


URL: /en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/ignoreCase
Title: RegExp.prototype.ignoreCase
on GitHub


URL: /en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/multiline
Title: RegExp.prototype.multiline
on GitHub

No new external URLs


URL: /en-US/docs/Web/JavaScript/Guide/Regular_Expressions
Title: Regular expressions
on GitHub

No new external URLs

(this comment was updated 2022-08-30 05:40:13.181025)

You cannot change this property directly. It is read-only.
- Any [Unicode codepoint escapes](/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Unicode_Property_Escapes) (`\u{xxxx}`, `\p{UnicodePropertyValue}`) will be interpreted as such instead of literal characters.
- Surrogate pairs will be interpreted as whole characters instead of two separate characters. For example, `/[😄]/u` would only match `"😄"` but not `"\ud83d"`.
- 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 codepoints instead of UTF-16 code units.
Copy link

@ghost ghost Aug 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I'd think that a warning about the mismatch between JavaScript string indexing with brackets vs. the index returned here, might be warranted?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lastIndex still returns the UTF-16 index. It just may advance by 2 in case of a surrogate pair.

@@ -1,5 +1,5 @@
---
title: RegExp.prototype.sticky
title: get RegExp.prototype.sticky
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooh, I have not seen get used in any MDN titles yet... isn't such an introduction pending the discussion about prototype accessors vs own properties?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, get is already used in @@species. I think it makes sense to update these.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was that broadly agreed in @@species or did it just just sneak in?

I have no care factor about what syntax we use, but it shouldn't appear by magic - it should be discussed (and not just in one PR). If agreed it should be updated in the contributor docs so that we're all on the same page. IN addition, we should identify all the cases that need updating and do them as a project.

If there was extensive discussion and this is all documented and well understood. Apologies. Don't know how I missed it.

I made a similar comment above - just linking so no need to respond in both places #19540 (comment)

Copy link
Member Author

@Josh-Cena Josh-Cena Aug 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@species seems to just have been like that from the start. Plus we just don't have an agreed-upon notation for any property. (cf mdn/mdn-community#153) I guess @@species was written like that because these pages are so niche that no one cares about them other than spec gurus :-)

I really don't have strong opinions about whether we prefix getter-only properties with get, but this seems like a really low-bar change compared to marking up instance properties. If others feel like the change is too bold I can revert it for now.


## Description

The value of `sticky` is a {{jsxref("Boolean")}} and 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). A regular expression defined as both `sticky` and `global` ignores the `global` flag.
`RegExp.prototype.sticky` is a getter-only property that returns `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).
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect that the term "getter-only" may not be friendly towards beginners. Any thoughts on this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fact that no-one complains up till now probably means these pages are non-starter from the beginning, so I guess it's fine. Most would read the guide anyway.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is "getter-only" different from read-only? i.e. it is far more common to see something like:

The RegExp.prototype.sticky is a read-only property returns true ...

And in the parent doc you'd have the {{readonly_inline}} macro

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not much. "Read-only" is for data properties, "getter-only" is for accessor properties; but when you use them they are pretty much interchangeable.

Copy link
Collaborator

@hamishwillee hamishwillee Aug 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just not sure it is a useful distinction. If it isn't, then better to make things consistent. If it is, then great - but we need to agree how and document that approach.

Edited. I see this has been added to the discussion. Let's finish this there.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's discuss in mdn/mdn-community#195. If others also feel strongly about this change I can revert it (at best aligning with what we do with other getter-only properties like %TypedArray%.p.length) so we can get the important pieces in.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely.

@@ -1,8 +1,7 @@
---
title: RegExp.prototype.dotAll
title: get RegExp.prototype.dotAll
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have never seen get used in a title in MDN.

Generally JavaScript has been treated as a little bit special in that it remains closer to spec language that the web api docs. In my strong opinion this might be fine, but it should be raised as a discussion and agreed, not added by magic.

@wbamberg might have an opinion

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Further, if we're going to change the rules, then they should be changed across the docset and reflected in the template guidance for javascript thingies.

It is possible this has been done of course, and I just missed it.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Further, if we're going to change the rules, then they should be changed across the docset and reflected in the template guidance for javascript thingies.

It is possible this has been done of course, and I just missed it.

See #19540 (comment)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I agree with Hamish that we should do this in an agreed and organized way. Without that I'm -1 on get here.

@Josh-Cena
Copy link
Member Author

I've hopefully made the content a little clearer and reverted the title changes. I intend to re-propose this sometime in the future when I have more energy.

@Josh-Cena
Copy link
Member Author

Minor ping for @wbamberg in case you have any feedback on this ^^ The wording change here is pretty important and it's a shame to be delayed so long (the issue has remained for a year and a half)

Copy link
Collaborator

@wbamberg wbamberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly copy-edits, though I had a bigger comment on the sticky page. But it's a use-it-or-not-as-you-wish type of thing.

Copy link
Collaborator

@wbamberg wbamberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 thank you @Josh-Cena !

@wbamberg wbamberg merged commit 7a1edd4 into mdn:main Aug 30, 2022
@Josh-Cena Josh-Cena deleted the fix-regex-flag branch August 30, 2022 21:21
goshdarnheck pushed a commit to goshdarnheck/content that referenced this pull request Sep 7, 2022
* Fix description of regex flags

* fix

* Fix source

* Better wording

* Add link

* Apply suggestions from code review

Co-authored-by: wbamberg <[email protected]>

* Add an exhaustive list

Co-authored-by: wbamberg <[email protected]>
@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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

RegExp - accessors are mis-documented as (own) properties Sticky flag doesn't entirely mask the global flag
3 participants