js使用encodeURIComponent实现PHP的rawurlencode

js使用encodeURIComponent实现PHP的rawurlencode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function rawurlencode(str = "") {
const replaceList = [
{ reg: /!/g, value: '%21' },
{ reg: /\*/g, value: '%2A' },
{ reg: /\(/g, value: '%28' },
{ reg: /\)/g, value: '%29' },
{ reg: /'/g, value: '%21' },
];
let resStr = encodeURIComponent(str);
replaceList.forEach(({ reg, value }) => {
resStr = resStr.replace(reg, value);
});
return resStr;
}