当我需要提取参数值的URL字符串中有URIError: malformed URI sequence?
符号%
时,60% - Completed
以下代码错误消失了,例如http://some-external-server.com/info?progress=60%%20-%20Completed
<SCRIPT type="text/javascript">
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
</SCRIPT>
我无法控制服务器,需要在html页面中处理输出。
答案 0 :(得分:4)
我认为您需要将百分号URI编码为'%25'
http://some-external-server.com/info?progress=60%25%20-%20Completed
[编辑]
我想你可以这样做:
var str = "60%%20-%20completed";
var uri_encoded = str.replace(/%([^\d].)/, "%25$1");
console.log(str); // "60%25%20-%20completed"
var decoded = decodeURIComponent(uri_encoded);
console.log(decoded); // "60% - completed"