Node.js-核心模块querystring

3. querystring

    querystring.escape()  ---编码

    Querystring.unescape()  ---escape的逆运算

    Querystring.parse()  ---解析路径,对象

    Querystring.stringify() parse的逆运算,对象变成字符串

例子:

// 导入模块
const queryString = require('querystring');
const {URL} = require('url');
// 处理查询信息
let urlValue = 'http://www.test.com/search?wd=hello&q=你好世界';
const url = new URL(urlValue);
console.log(url.search);//?wd=hello&q=%E4%BD%A0%E5%A5%BD%E4%B8%96%E7%95%8C
console.log(queryString.escape(url.search));
//%3Fwd%3Dhello%26q%3D%25E4%25BD%25A0%25E5%25A5%25BD%25E4%25B8%2596%25E7%2595%258C
console.log(queryString.unescape('%3Fwd%3Dhello%26q%3D%25E4%25BD%25A0%25E5%25A5%25BD%25E4%25B8%2596%25E7%2595%258C'));
//?wd=hello&q=%E4%BD%A0%E5%A5%BD%E4%B8%96%E7%95%8C
console.log(queryString.parse(url.search));//输出带着?号:{ '?wd': 'hello', q: '你好世界' }
 const sp = url.searchParams;
 console.log(sp);//URLSearchParams { 'wd' => 'hello', 'q' => '你好世界' }
 // 获取sp中的值
 console.log(sp.get('wd'));
 // 遍历sp信息
 sp.forEach(function (val,key) {
    console.log(key,val);//wd hello
                        //q 你好世界
});

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值