urlPattern是用于匹配URL的新Web API。它的目的是为Web开发人员提供方便的API,并在需要匹配URL的其他Web API中可用;例如服务工作者。解释器讨论了激励的用例。
这是urlpattern API的多填充,因此该功能可在不本地支持的浏览器中可用。该多填充通过相同的Web平台测试套件。
多文件可在浏览器(ESM模块)和node.js中工作。
仅当urlpattern在全局对象上尚未存在时,才会加载多填充,在这种情况下,它将其添加到全局对象中。
// Conditional ESM module loading (Node.js and browser)
// @ts-ignore: Property 'UrlPattern' does not exist
if ( ! globalThis . URLPattern ) {
await import ( "urlpattern-polyfill" ) ;
}
/**
* The above is the recommended way to load the ESM module, as it only
* loads it on demand, thus when not natively supported by the runtime or
* already polyfilled.
*/
import "urlpattern-polyfill" ;
/**
* In case you want to replace an existing implementation with the polyfill:
*/
import { URLPattern } from "urlpattern-polyfill" ;
globalThis . URLPattern = URLPattern 笔记:
在某些环境中,
// @ts-ignore: Property 'UrlPattern' does not exist的行:在加载polyfill之前,可能无法可用,并且IF语句中的功能检查会给出打字错误错误。整个想法是,当它不存在时,它会加载。
// Conditional CJS module loading (Node.js)
if ( ! globalThis . URLPattern ) {
require ( "urlpattern-polyfill" ) ;
}
/**
* The above is the recommended way to load the CommonJs module, as it only
* loads it on demand, thus when not natively supported by the runtime or
* already polyfilled.
*/
require ( "urlpattern-polyfill" ) ;
/**
* In case you want to replace an existing implementation with the polyfill:
*/
const { URLPattern } = require ( "urlpattern-polyfill" ) ; ;
globalThis . URLPattern = URLPattern 笔记:
无论您如何加载polyfill,如果您的环境中没有实现,它将始终将其添加到全局对象中。
let p = new URLPattern ( { pathname : '/foo/:name' } ) ;
let r = p . exec ( 'https://example.com/foo/bar' ) ;
console . log ( r . pathname . input ) ; // "/foo/bar"
console . log ( r . pathname . groups . name ) ; // "bar"
let r2 = p . exec ( { pathname : '/foo/baz' } ) ;
console . log ( r2 . pathname . groups . name ) ; // "baz" // Match same-origin jpg or png URLs.
// Note: This uses a named group to make it easier to access
// the result later.
const p = new URLPattern ( {
pathname : '/*.:filetype(jpg|png)' ,
baseURL : self . location
} ) ;
for ( let url in url_list ) {
const r = p . exec ( url ) ;
// skip non-matches
if ( ! r ) {
continue ;
}
if ( r . pathname . groups [ 'filetype' ] === 'jpg' ) {
// process jpg
} else if ( r . pathname . groups [ 'filetype' ] === 'png' ) {
// process png
}
}在这种情况下,可以通过剩下baseurl来使模式更简单,而无需原点检查。
// Match any URL ending with 'jpg' or 'png'.
const p = new URLPattern ( { pathname : '/*.:filetype(jpg|png)' } ) ; 我们计划还支持用于初始化urlpattern对象的“简短表格”。这得到了多填充的支持,但尚未得到铬的实施。
例如:
const p = new URLPattern ( "https://*.example.com/foo/*" ) ;或者:
const p = new URLPattern ( "foo/*" , self . location ) ;下面找到带有打字稿类型注释的API概述。相关的浏览器Web IDL可以在此处找到。
type URLPatternInput = URLPatternInit | string ;
class URLPattern {
constructor ( init ?: URLPatternInput , baseURL ?: string ) ;
test ( input ?: URLPatternInput , baseURL ?: string ) : boolean ;
exec ( input ?: URLPatternInput , baseURL ?: string ) : URLPatternResult | null ;
readonly protocol : string ;
readonly username : string ;
readonly password : string ;
readonly hostname : string ;
readonly port : string ;
readonly pathname : string ;
readonly search : string ;
readonly hash : string ;
}
interface URLPatternInit {
baseURL ?: string ;
username ?: string ;
password ?: string ;
protocol ?: string ;
hostname ?: string ;
port ?: string ;
pathname ?: string ;
search ?: string ;
hash ?: string ;
}
interface URLPatternResult {
inputs : [ URLPatternInput ] ;
protocol : URLPatternComponentResult ;
username : URLPatternComponentResult ;
password : URLPatternComponentResult ;
hostname : URLPatternComponentResult ;
port : URLPatternComponentResult ;
pathname : URLPatternComponentResult ;
search : URLPatternComponentResult ;
hash : URLPatternComponentResult ;
}
interface URLPatternComponentResult {
input : string ;
groups : {
[ key : string ] : string | undefined ;
} ;
}这里的模式语法基于流行的通道通道库中使用的语法。
"/"字符。":"字符,然后是名称开头。例如, "/:foo/:bar"有两个命名组。"/:foo(.*)"将覆盖与下一个分隔线匹配的默认值。"?" , "*"或"+"的功能,就像在正则表达式中一样。当组是可选的或重复的,并且先于分隔器,则分隔线也是可选的或重复的。例如, "/foo/:bar?"将匹配"/foo" , "/foo/"或"/foo/baz" 。逃脱的分隔线将使之改为需要。"(.*)"目前,我们计划与regexp的这些已知差异:
URL具有基于ASCII的规范形式,这意味着国际化域名(主机名)也具有基于规范的ASCII表示的表示,并且其他组件(例如hash , search和pathname使用百分比编码进行编码。
当前, URLPattern不执行模式的任何编码或标准化。因此,开发人员将需要在将模式传递到构造函数中之前编码Unicode字符。同样,构造函数也不执行诸如 /foo/../bar to /bar之类的扁平路径名。当前,必须将模式写入目标规范URL输出。
但是,它确实执行了这些操作以进行test()和exec()输入。
编码组件可以轻松手动完成,但不要编码模式语法:
encodeURIComponent ( "?q=æøå" )
// "%3Fq%3D%C3%A6%C3%B8%C3%A5" new URL ( "https://ølerlækkernårdetermit.dk" ) . hostname
// "xn--lerlkkernrdetermit-dubo78a.dk"如果您在https://github.com/intel上拥有有关安全问题或漏洞的信息,请发送电子邮件至[email protected]。使用我们的PGP公钥加密敏感信息。有关与英特尔产品有关的问题,请访问https://security-center.intel.com。