url处理:encodeURIComponent和URLSearchParams

1、encodeURIComponent

网址:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent

encodeURIComponent()函数通过将一个,两个,三个或四个表示字符的UTF-8编码的转义序列替换某些字符的每个实例来 UTF-8 编码 URI(对于由两个“代理”字符组成的字符而言,将仅是四个转义序列) 。

不转义字符:A-Z a-z 0-9 - _ . ! ~ * ' ( )

encodeURIComponent() 和 encodeURI 有以下几个不同点:

var set1 = ";,/?:@&=+$";  // 保留字符
var set2 = "-_.!~*'()";   // 不转义字符
var set3 = "#";           // 数字标志
var set4 = "ABC abc 123"; // 字母数字字符和空格

console.log(encodeURI(set1)); // ;,/?:@&=+$
console.log(encodeURI(set2)); // -_.!~*'()
console.log(encodeURI(set3)); // #
console.log(encodeURI(set4)); // ABC%20abc%20123 (the space gets encoded as %20)

console.log(encodeURIComponent(set1)); // %3B%2C%2F%3F%3A%40%26%3D%2B%24
console.log(encodeURIComponent(set2)); // -_.!~*'()
console.log(encodeURIComponent(set3)); // %23
console.log(encodeURIComponent(set4)); // ABC%20abc%20123 (the space gets encoded as %20)

 

2、URLSearchParams

网址:https://developer.mozilla.org/zh-CN/docs/Web/API/URLSearchParams

URLSearchParams接口定义了一些实用的方法来处理URL的查询字符串。

URLSearchParams.append()  //插入一个指定的键/值对作为新的搜索参数。

URLSearchParams.delete()   //从搜索参数列表里删除指定的搜索参数及其对应的值

URLsearchParams.entries()   //返回一个iterator可以遍历所有键/值对的对象。

URLsearchParams.get()   //获取指定搜索参数的第一个值。

URLsearchParams.getAll()   //获取指定搜索参数的所有值,返回是一个数组。

URLsearchParams.has()   //返回 Boolean 判断是否存在此搜索参数。

URLsearchParams.keys()   //返回iterator 此对象包含了键/值对的所有键名。

URLsearchParams.set()    //设置一个搜索参数的新值,假如原来有多个值将删除其他所有的值。

URLsearchParams.sort()    //按键名排序。

URLsearchParams.toString()    //返回搜索参数组成的字符串,可直接使用在URL上。 

URLsearchParams.values()    //返回iterator 此对象包含了键/值对的所有值。

使用:

// 如果需要支持较低版本的 PC 浏览器,需要额外安装和引用 polyfill:
// npm install url-search-params-polyfill --save
// import 'url-search-params-polyfill';

// $ npm install --save url-search-params
// import URLSearchParams from 'url-search-params';

const urlSearchParams = new URLSearchParams();

例子:

var paramsString = "q=URLUtils.searchParams&topic=api"
var searchParams = new URLSearchParams(paramsString);

for (let p of searchParams) {
  console.log(p);
}

searchParams.has("topic") === true; // true
searchParams.get("topic") === "api"; // true
searchParams.getAll("topic"); // ["api"]
searchParams.get("foo") === null; // true
searchParams.append("topic", "webdev");
searchParams.toString(); // "q=URLUtils.searchParams&topic=api&topic=webdev"
searchParams.set("topic", "More webdev");
searchParams.toString(); // "q=URLUtils.searchParams&topic=More+webdev"
searchParams.delete("topic");
searchParams.toString(); // "q=URLUtils.searchParams"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在JavaScript中,原生的`URLSearchParams`对象是用于处理查询参数的,特别是从URL中解析和构造查询字符串。如果你想要替换它的功能,可以使用自定义函数来模拟类似的行为。这里是一个简单的例子,展示如何手动创建一个类似`URLSearchParams`的方法: ```javascript function customSearchParams(params) { let obj = {}; if (typeof params === 'string') { params = new URLSearchParams(params); } else if (!Array.isArray(params)) { throw new Error('Invalid input, expecting an array or a string'); } for (let [key, value] of params.entries()) { obj[key] = Array.isArray(value) ? value : [value]; } return { append: function(key, value) { obj[key].push(value); return this; }, delete: function(key) { if (obj.hasOwnProperty(key)) { obj[key] = []; } return this; }, get: function(key) { return obj[key] || null; }, getAll: function(key) { return obj[key] || []; }, has: function(key) { return obj.hasOwnProperty(key); }, set: function(key, value) { obj[key] = [value]; return this; }, toString: function() { let result = []; for (let key in obj) { for (let value of obj[key]) { result.push(`${encodeURIComponent(key)}=${encodeURIComponent(value)}`); } } return result.join('&'); } }; } // 使用示例 const urlParams = customSearchParams('key1=value1&key2=value2'); console.log(urlParams.get('key1')); // 输出 "value1" urlParams.append('key1', 'value1a'); console.log(urlParams.toString()); // 输出 "key1=value1&key1=value1a&key2=value2" ``` 这个自定义函数实现了`append`, `delete`, `get`, `getAll`, `has`, `set`和`toString`等主要方法,但请注意,这只是一个简化版本,可能不如`URLSearchParams`完全兼容浏览器行为。如果你需要更复杂的查询字符串操作,还是推荐使用原生的`URLSearchParams`。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值