mdn / content

The content behind MDN Web Docs
https://developer.mozilla.org
Other
9.24k stars 22.49k forks source link

All lookaheads and lookbehinds examples are wrong #27864

Open timonson opened 1 year ago

timonson commented 1 year ago

MDN URL

https://developer.mozilla.org/en-US/docs/Web/API/URL_Pattern_API#regex_matchers_limitations

What specific section or headline is this issue about?

Lookaheads, and lookbehinds will never match any portion of the URLPattern.

What information was incorrect, unhelpful, or incomplete?

All lookaheads and lookbehinds examples are wrong. Let's take a look at the negative-lookahead examples:

 // negative-lookahead
const pattern = new URLPattern({ pathname: "(a(?!b))" });
console.log(pattern.test("https://example.com/ab")); // false
console.log(pattern.test("https://example.com/ax")); // false

There are two problems in the docs:

What did you expect to see?

 // negative-lookahead
const pattern = new URLPattern({ pathname: "/(a(?!b).*)" });
console.log(pattern.test("https://example.com/ab")); // false
console.log(pattern.test("https://example.com/ax")); // true

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

https://www.regular-expressions.info/lookaround.html

Do you have anything more you want to share?

No response

OnkarRuikar commented 1 year ago

Regarding the first example in the section

Starts with ^ will only match if used at the start of the protocol portion of the URLPattern and is redundant if used.

But following works

let pattern = new URLPattern({ pathname: "(^/ba)" });
console.log(pattern.test("http:/exb.com/ba"));
// true