mdn / interactive-examples

Home of the MDN live code editor interactive examples
Creative Commons Zero v1.0 Universal
725 stars 507 forks source link

`RegExp.prototype.unicode` examples are not helpful #2490

Open Marcono1234 opened 1 year ago

Marcono1234 commented 1 year ago

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.

[^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');

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

No response

Do you have anything more you want to share?

No response

github-actions[bot] commented 1 year ago

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 commented 1 year ago

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 https://github.com/mdn/content/pull/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.