encodeURIComponent函数。这个函数会对URL的组成部分进行编码,将左括号转换为%28,将右括号转换成%29,我在编辑器尝试这么做,但中英文括号都是原样输出(可能我这个编辑器不生效),所以就想到手动替换的方式,给大家分享这个方法:
function encodeBrackets(text) {
return text.replace(/\(|\)/g, function(match) {
switch(match) {
case "(": return "%28";
case ")": return "%29";
default: return match;
}
});
}
console.log('---------',encodeBrackets("今天是个(好日子)"))
这样左括号就提换成了%28,有括号替换成了%29