/**
* 正则Group匹配
* 匹配url中 :id 等占位符字段
*/
function getMatches (string) {
let matches = []
let regex = /(:[a-z_]+)/
let match = regex.exec(string)
while (match !== null) {
matches.push(match[1])
string = string.replace(match[1], '')
match = regex.exec(string)
}
return matches
}
console.log(getMatches("http://test.com/users/:id"))
// 输出结果
// :id
/**
* 从URL匹配出参数pathParams
* @param {string} url
* @param {object} params
* @returns {string}
* @private
*/
_wrapUrl (url, params) {
if (params !== null) {
let matches = this._getMatches(url)
for (let match of matches) {
let value = params[match.replace(':', '')]
if (value !== null) {
url = url.replace(match, value)
}
}
}
return this.path + url
},