url-match-pattern
v1.0.1
Published
handle match patterns described in browser extension
Downloads
6
Readme
URL Match Pattern
a JavaScript package handles match patterns described in browser extension
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
.
scheme | this project | Firefox 55+ | Chrome 91+ | Edge, Opera | Safari
----------------|--------------|-------------|------------|-------------|------
ws
, wss
| Yes | Yes | No | No | No
urn
| No | No | Yes | No | No
data
| No | partial | No | No | No
file
| Yes | Yes | Yes | Yes | No
Defects of URL Match Pattern
- not well-defined
- no escaping way for
*
in path *
in path also matches/
and?
, which usually have special meaning.