varpathToRegexp = require('path-to-regexp') varurl ='/user/:id/:name' vardata = {id: 10001, name:'bob'} console.log(pathToRegexp.compile(url)(data)) 打印结果: 1 /user/10001/bob const pathToRegExp = require('path-to-
1:path-to-regexp.js 源码分析如下: 首先从源码中该js文件对外暴露了5个方法,源码如下: module.exports = pathToRegexp module.exports.parse = parse module.exports.compile = compile module.exports.tokensToFunction = tokensToFunction module.exports.tokensToRegExp = tokensToRegExp 首先要说明下的是:分析...
const pathToRegexp = require('path-to-regexp') var regexp_1 = pathToRegexp('/foo/:bar')// /^\/foo\/([^\/]+?)(?:\/)?$/i regexp_1.exec('/foo/barrrr')//匹配成功 =>RegExpExecArray [ '/foo/barrrr', 'barrrr', index: 0, input: '/foo/barrrr' ] regexp_1.exec('...
const{regexp,keys}=pathToRegexp("/foo/:bar"); Compile ("Reverse" Path-To-RegExp) Thecompilefunction will return a function for transforming parameters into a valid path: pathA string. options(Seeparsefor more options) delimiterThe default delimiter for segments, e.g.[^/]for:namedparameters...
一:path-to-regexp.js 源码分析如下: 首先从源码中该js文件对外暴露了5个方法,源码如下: module.exports =pathToRegexp module.exports.parse=parse module.exports.compile=compile module.exports.tokensToFunction=tokensToFunction module.exports.tokensToRegExp= tokensToRegExp ...
js 中有 RegExp 方法做正则表达式校验,而 path-to-regexp 可以看成是 url 字符串的正则表达式。 使用 第三方库,使用前先进行安装: $ npm install path-to-regexp 1. 在js 中使用: const pathToRegexp = require('path-to-regexp'); 1.
Path-to-RegExp Turn a path string such as/user/:nameinto a regular expression. Installation npm install path-to-regexp --save Usage const{match,pathToRegexp,compile,parse,stringify,}=require("path-to-regexp"); Parameters Parameters match arbitrary strings in a path by matching up to the ...
input: '/foo/route' ]varpathToRegexp=require('path-to-regexp');varurl='/user/:id';console.log(pathToRegexp.parse(url));varpathToRegexp=require('path-to-regexp')varurl='/user/:id/:name'vardata={id:10001,name:'bob'}console.log(pathToRegexp.compile(url)(data))//user/10001/bob...
console.log(pathToRegexp.parse(url)); var pathToRegexp = require('path-to-regexp') var url = '/user/:id/:name' var data = {id: 10001, name: 'bob'} console.log(pathToRegexp.compile(url)(data))//user/10001/bob 1. 2. ...
const toPathRegexp = compile("/user/:id(\\d+)"); // When disabling `encode`, you need to make sure inputs are encoded correctly. No arrays are accepted. const toPathRaw = compile("/user/:id", { encode: false }); toPathRegexp({ id: "123" }); //=> "/user/123" toPath...