Skip to content
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

RegExp.prototype.unicode examples are not helpful #2490

Open
Marcono1234 opened this issue Mar 21, 2023 · 2 comments
Open

RegExp.prototype.unicode examples are not helpful #2490

Marcono1234 opened this issue Mar 21, 2023 · 2 comments
Labels
Content:JS issues related to JavaScript examples. idle Issues and pull requests with no activity for three months.

Comments

@Marcono1234
Copy link

What information was incorrect, unhelpful, or incomplete?

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/unicode

The current example just shows how to access the property, it does not show at all what effect the Regex flag has.

What did you expect to see?

The example should show what difference the u flag has, at the very least something like:

const regex1 = new RegExp('\\u{61}');
const regex2 = new RegExp('\\u{61}', 'u');

console.log(regex1.unicode);
// Expected output: false

console.log(regex2.unicode);
// Expected output: true

console.log(regex1.test("a"));
// Expected output: false

console.log(regex2.test("a"));
// Expected output: true

However, the \\u{...} example might be a bit pointless because you can just write \u{...} which is understood uses a JavaScript escape sequence and therefore works regardless of whether the u flag is used or not (I assume).1

A more interesting example might be to match a Unicode property, and / or to show that matching emojis (respectively in general any supplementary character) only works correctly when using the u flag.

Do you have any supporting links, references, or citations?

No response

Do you have anything more you want to share?

No response

Footnotes

  1. This also highlights a bug with the current example; the \u{61} is (if I understand it correctly) a normal JavaScript escape, so the current example is equivalent to:

    const regex1 = new RegExp('a');
    const regex2 = new RegExp('a', 'u');
    
@Marcono1234 Marcono1234 added the needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. label Mar 21, 2023
@github-actions
Copy link

It looks like this is your first issue. Welcome! 👋 One of the project maintainers will be with you as soon as possible. We appreciate your patience. To safeguard the health of the project, please take a moment to read our code of conduct.

@Josh-Cena
Copy link
Member

is understood uses a JavaScript escape sequence

\u{61} would be a string literal escape sequence; \\u{61} is the string literal form of /\u{61}/, which is actually a regex syntax of Unicode escape, which only works in u mode.

Ideally, we would have a separate page for the u flag (as part of mdn/content#22210), but I'm probably not going to do that in the end, so it makes sense to make the unicode example a bit more useful.

@Josh-Cena Josh-Cena added Content:JS issues related to JavaScript examples. and removed needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. labels Apr 4, 2023
@github-actions github-actions bot added the idle Issues and pull requests with no activity for three months. label May 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Content:JS issues related to JavaScript examples. idle Issues and pull requests with no activity for three months.
Projects
None yet
Development

No branches or pull requests

2 participants