url模版正则替换

-

 

const templateRe = /\{ *([\w_ -]+) *\}/g;

export function template(str, data) {
    return str.replace(templateRe, (str, key) => {
        let value = data[key];

        if (value === undefined) {
            throw new Error(`No value provided for variable ${str}`);

        } else if (typeof value === 'function') {
            value = value(data);
        }
        return value;
    });
}

 

 

 

-

posted @ 2025-02-26 21:28  古墩古墩  Views(11)  Comments(0)    收藏  举报