eightHundreds
5/27/2017 - 12:25 PM

把形如这样的 url abc.com/{a} param {a:1,b:2} 变成 url:abc.com/1 param:{b:2}

把形如这样的 url abc.com/{a} param {a:1,b:2} 变成 url:abc.com/1 param:{b:2}

	var renderUrl=function (url, params) {
        const regex = /\/(?:\{(\w+)\})/g;
        var m;
        var result = url;
        while ((m = regex.exec(url)) !== null) {
            if (m.index === regex.lastIndex) {
                regex.lastIndex++;
            }
            if (params[m[1]] !== undefined) {
                result = result.replace('{' + m[1] + '}', params[m[1]])
                delete params[m[1]]
            } else {
                console.log('params', params)
                throw Error(`在解析请求url时有未匹配的属性${m[1]}`)
            }
        }
        return { url: result, params: params };
    }