鍍金池/ 問(wèn)答/HTML/ encodeURI 怎么只把url中的漢字轉(zhuǎn)碼

encodeURI 怎么只把url中的漢字轉(zhuǎn)碼

因?yàn)榉N種原因

let url = '/api/filter/getPage/90000/BILL?at=/layoutContent/03/page&title=采購(gòu)申請(qǐng)&subTitle=采購(gòu)申請(qǐng)一覽'

只把url中的漢字encodeURI轉(zhuǎn)碼

回答
編輯回答
心悲涼

function encodeChineseOnly(str){

        return encodeURI(str).replace(/([^\u0000-\u00FF])/g,escape)
    }
2017年4月1日 11:13
編輯回答
我不懂
let url = '/api/filter/getPage/90000/BILL?at=/layoutContent/03/page&title=采購(gòu)申請(qǐng)&subTitle=采購(gòu)申請(qǐng)一覽'

function encodeURIForChinese(url) {
    let chineseArray = url.match(/[^\x00-\xff]+/ig);
    for (let i = chineseArray.length - 1; i >= 0; i--) {
        url = url.replace(chineseArray[i], encodeURIComponent(chineseArray[i]));
    }
    return url;
}
console.log(encodeURIForChinese(url));
2017年1月9日 16:12
編輯回答
墨沫
url.replace(/([^\u0000-\u00FF])/g, function ($) { return encodeURI($)})
2018年3月26日 15:09