1.0.1 • Published 3 years ago

url-match-pattern v1.0.1

Weekly downloads
-
License
MIT
Repository
-
Last release
3 years ago

URL Match Pattern

a JavaScript package handles match patterns described in browser extension

demo page

See articles on MDN and Chrome Developers.

Usage

/// check if a string is a valid URL match pattern
URLMatchPattern.test('*://github.com/'); // true

/// check if a string matches a URL match pattern
URLMatchPattern.test('*://*.google.com/*', 'https://google.com/search'); // true

/// get a RegExp object which represents a URL match pattern
const regexp = URLMatchPattern.toRegExp('*://*.name/');
regexp.test('https://domainA.name/'); // true
regexp.test('https://domainB.name/'); // true

/// or create an object with `test` method
const pattern = new URLMatchPattern('*://*.name/');
pattern.test('https://domainA.name/'); // true
pattern.test('https://domainB.name/'); // true

Note

Method test (both static and instance) allows url string:

  • with port
  • with hash
  • without path

However, that's not the case after converting to RegExp.

URLMatchPattern.test('*://*/', 'http://username:password@abc:666/#hash=hash?a'); // true
URLMatchPattern.test('*://*/', 'http://abc'); // true

const pattern = new URLMatchPattern('*://*/');
pattern.test('http://username:password@abc:666/#hash=hash?a'); // true
pattern.test('http://abc'); // true

/// converting to RegExp object would lose that compatibility for current version
const regexp = URLMatchPattern.toRegExp('*://*/');
regexp.test('http://abc/'); // true
regexp.test('http://abc:80/'); // false
regexp.test('http://abc/#foo'); // false
regexp.test('http://abc'); // false

Compatibility

Either browser supports http, https, and ftp.

schemethis projectFirefox 55+Chrome 91+Edge, OperaSafari
ws, wssYesYesNoNoNo
urnNoNoYesNoNo
dataNopartialNoNoNo
fileYesYesYesYesNo

Defects of URL Match Pattern

  • not well-defined
  • no escaping way for * in path
  • * in path also matches / and ?, which usually have special meaning.