js使用encodeURIComponent实现PHP的rawurlencode

发布: 2020-06-11 16:21:10标签: 前端开发

js使用encodeURIComponent实现PHP的rawurlencode

01function rawurlencode(str = "") {
02 const replaceList = [
03 { reg: /!/g, value: '%21' },
04 { reg: /\*/g, value: '%2A' },
05 { reg: /\(/g, value: '%28' },
06 { reg: /\)/g, value: '%29' },
07 { reg: /'/g, value: '%21' },
08 ];
09 let resStr = encodeURIComponent(str);
10 replaceList.forEach(({ reg, value }) => {
11 resStr = resStr.replace(reg, value);
12 });
13 return resStr;
14}
复制代码