encodeURIComponent() 小写

问题描述

在 JavaScript 中,我想使用 encodeURIComponent()输出是大写的。

console.log(encodeURIComponent("[Abcd123]"));
//returns %5BAbcd123%5D

我想要一个小写的结果

%5bAbcd123%5d

有可能吗?

解决方法

您可以使用正则表达式将 % 后的两个字符转换为小写。

console.log(encodeURIComponent("[Abcd123]").replace(/%../g,match => match.toLowerCase()));