// 示例代码:使用 JavaScript 解码 URL 编码的字符串
// 使用 decodeURIComponent 方法解码 URL 编码的字符串
function decodeUrl(encodedString) {
try {
// decodeURIComponent 会将编码后的 URL 字符串解码为原始字符串
return decodeURIComponent(encodedString);
} catch (e) {
// 如果解码失败,返回错误信息
return "解码失败: " + e.message;
}
}
// 示例用法
const encodedString = "https%3A%2F%2Fexample.com%2Fpath%3Fquery%3Dvalue";
const decodedString = decodeUrl(encodedString);
console.log(decodedString); // 输出: https://example.com/path?query=value
decodeURIComponent 是 JavaScript 中用于解码 URL 编码字符串的内置函数。它会将 %XX 格式的编码字符转换为对应的字符。decodeUrl 函数,该函数接受一个编码后的字符串作为参数,并尝试使用 decodeURIComponent 进行解码。上一篇:js split 多个分隔符
下一篇:js 去除字符串中所有空格
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站