一个完整的URL字符串中,从"?"(不包括?)到"#"(如果存在#)或者到该URL字符串结束(如果不存在#)的这一部分称为查询字符串.

可以使用Query String模块中的parse方法将该字符串转换为一个对象,parse方法的使用方式如下所示:

querystring.parse(str,[sep],[eq],[options]);

str表示被转换的查询字符串,

sep.字符串中的分隔符,默认是&

eq.该字符串中的分配符,默认为=."="左边是key,右边是value

options:是一个对象,可以在该对象中使用一个整数值类型的maxKeys属性来指定转换后的对象中的属性个数,如果将maxKeys属性值设定为0.其效果等于不使用maxKeys属性值

 var querystring=require("querystring");
var str="username=guoyansi&age=40&sex=male";
var res=querystring.parse(str);
console.log("1:%j",res);//1:{"username":"guoyansi","age":"40","sex":"male"}
res=querystring.parse(str,"!");
console.log("2:%j",res);//2:{"username":"guoyansi&age=40&sex=male"}
res=querystring.parse(str,"&");
console.log("3:%j",res);//3:{"username":"guoyansi","age":"40","sex":"male"} str="username=guoyansi!age=40!sex=male";
res=querystring.parse(str,"!");
console.log("4:%j",res);//4:{"username":"guoyansi","age":"40","sex":"male"}
res=querystring.parse(str,"!","=");
console.log("5:%j",res);//5:{"username":"guoyansi","age":"40","sex":"male"}
res=querystring.parse(str,"!",":");
console.log("6:%j",res);//6:{"username=guoyansi":"","age=40":"","sex=male":""}
res=querystring.parse(str,"!","=",{maxKeys:2});
console.log("7:%j",res);//7:{"username":"guoyansi","age":"40"}

stringify是将字符串转化成查询字符串的格式.

querystring.stringify(obj,[sep],[eq])

 var querystring=require("querystring");
var res= querystring.stringify({"username":"guoyansi","age":"40","sex":"male"});
console.log(res);//username=guoyansi&age=40&sex=male
res=querystring.stringify({"username":"guoyansi","age":"40","sex":"male"},"!");
console.log(res);//username=guoyansi!age=40!sex=male
res=querystring.stringify({"username":"guoyansi","age":"40","sex":"male"},"&",":");
console.log(res);//username:guoyansi&age:40&sex:male
res=querystring.stringify({"username":"guoyansi","age":["40","24"]},"&","=");
console.log(res);//username=guoyansi&age=40&age=24

在url模块中,可以使用parse()方法将URL字符串转换为一个对象,根据URL字符串中的不同内容,该对象可能具有的属性及其含义如下.

href:被转换的原URL字符串.

protocol:客户端发出请求时使用的协议.

slashes:在协议与路径中间时候使用"//"分隔符.

host:URL字符串中的完整地址及端口号,该地址可能为一个IP地址,也可能为一个主机名.

auth:URL字符串中的认证信息部分.

hostname:URL字符串中的完整地址,该地址可能为一个IP地址,也可能为一个主机名.

search:Url字符串中的查询字符串,包含起始字符"?"

path:url字符串中的路径,包含查询字符串.

query:url字符串中的查询字符串,不包含起始字符"?",或根据该查询字符串而转换的对象(根据parse()方法所用参数而决定query属性值);

hash:url字符串中的散列字符串,包含起始字符"#".

url.parse(urlstr,[parseQueryString]);

urlStr:是需要转换的URL字符串,

parseQueryString:是一个布尔值,当参数为true时,内部使用querystring模块查询字符串转换为一个对象,参数值为false时不执行该转换操作,默认是false

 var url=require("url");
var str="http://user:pass@host,com:8080/users/user.php?username=sisi&age=24&sex=male#name1";
var res=url.parse(str);
console.log(res);

{ protocol: 'http:',
  slashes: true,
  auth: 'user:pass',
  host: 'host:8080',
  port: '8080',
  hostname: 'host',
  hash: '#name1',
  search: '?username=sisi&age=24&sex=male',
  query: 'username=sisi&age=24&sex=male',
  pathname: '/,com/users/user.php',
  path: '/,com/users/user.php?username=sisi&age=24&sex=male',
  href: 'http://user:pass@host:8080/,com/users/user.php?username=sisi&age=24&sex=male#name1' }

 var url=require("url");
var str="http://user:pass@host,com:8080/users/user.php?username=sisi&age=24&sex=male#name1";
var res=url.parse(str,true);
console.log(res);

{ protocol: 'http:',
  slashes: true,
  auth: 'user:pass',
  host: 'host:8080',
  port: '8080',
  hostname: 'host',
  hash: '#name1',
  search: '?username=sisi&age=24&sex=male',
  query: { username: 'sisi', age: '24', sex: 'male' },
  pathname: '/,com/users/user.php',
  path: '/,com/users/user.php?username=sisi&age=24&sex=male',
  href: 'http://user:pass@host:8080/,com/users/user.php?username=sisi&age=24&sex=male#name1' }

第一个例子和第二个例子不同之处在于parse的第二个参数,导致了结果中的query的不同

可以将一个url转换过的对象转换成一个url字符串.

 var url=require("url");
var str="http://user:pass@host,com:8080/users/user.php?username=sisi&age=24&sex=male#name1";
var res=url.parse(str,true);
console.log(url.format(res));

结果是:

http://user:pass@host:8080/,com/users/user.php?username=sisi&age=24&sex=male#name1

node中转换URL字符串与查询字符串的更多相关文章

  1. SQL零星技术点:SQL中转换money类型数值转换为字符串问题

    --SQL中转换money类型数值转换为字符串问题,直接转换就转为两位了,所以需要做一下处理.具体请看下述sql实例. 1 create table #test(price money) insert ...

  2. 向现有URL末尾添加查询字符串参数

    向现有URL末尾添加查询字符串参数 xhr.open("get", "example.php?name1=value1&name2=value2", t ...

  3. vue中的锚点和查询字符串

    1.什么是锚点 锚点(achor):其实就是超链接的一种. 如:普通的超链接 <a href="sub_task_detail.php">详细</a>  主 ...

  4. 逐个访问URL的每个查询字符串参数

    下面介绍一个函数,用于处理location.search的结果,以解析查询字符串,然后返回包含所有参数的一个对象. 比如  www.baidu.com?q=javascript&num=10 ...

  5. # node中的url常用方法解析

    url字符串是一个结构化的字符串,由好几个有意义部分组成.我们在工作中不可避免的会用到其中的某个部分,最原始的通过字符串截取和正则匹配的方法难免用起来会不太方便和美观,所以在我们的nodejs中提供了 ...

  6. node中的url模块解析结果

    1. URL模块作用: url 模块用于处理与解析 URL. 使用方法如下: const url = require('url'); 2. URL 字符串与 URL 对象 URL 字符串是结构化的字符 ...

  7. mybatis中使用mysql的模糊查询字符串拼接(like)

    方法一: <!-- 根据hid,hanme,grade,模糊查询医院信息--> 方法一: List<Hospital> getHospitalLike(@Param(" ...

  8. 解析TextView中的URL等指定特殊字符串与点击事件

    使用TextView时,有时可能需要给予TextView里的特定字符串,比如URL,数字特别的样式,必希望能够添加点击事件.比如发短信时,文字里的url就可以点击直接打开浏览器,数字可以点击拨打电话. ...

  9. 使用 htaccess 重写 url,隐藏查询字符串

    例如我们有如下 URL: http://example.com/users.php?name=tania 但是我们想要让 URL 变成如下: http://example.com/users/tani ...

随机推荐

  1. Makefile中怎么使用Shell if判断

    /********************************************************************* * Makefile中怎么使用Shell if判断 * 说 ...

  2. 实战maven私有仓库三部曲之一:搭建和使用

    在局域网内搭建maven私有仓库,可避免每次都从中央仓库下载公共jar包,另外将A模块作为二方库发布到私有仓库后,B模块可以很方便的引用,今天我们就来实战maven私有仓库的搭建和使用: 原文地址:h ...

  3. tensorflow图像基本处理

    tensorflow库提供的专门的图片处理库,以下只是部分示例,更多函数请参照源码'\tensorflow_api\v1\image__init__.py' 加载图像 方式1: 使用tf.gfile. ...

  4. 每天一个linux命令:【转载】ls命令

    ls命令是linux下最常用的命令.ls命令就是list的缩写,缺省下ls用来打印出当前目录的清单,如果ls指定其他目录,那么就会显示指定目录里的文件及文件夹清单. 通过ls 命令不仅可以查看linu ...

  5. 在 Windows 安装期间将 MBR 磁盘转换为 GPT 磁盘

    以 UEFI 启动的 Windows 磁盘必须是 GPT 格式.本文将介绍如何在安装 Windows 期间将磁盘从 MBR 转换成 GPT. 特别注意:操作不慎可能丢失所有数据,如果你懂得安装系统的一 ...

  6. eclipse显示/隐藏代码行号

    Window→Preferences→General→Editors→TextEditors→勾选Show line numbers

  7. Java--数组转成list,list转数组

    数组转成list: 方法一: String[] userid = {"aa","bb","cc"}; List<String> ...

  8. 解决js代码中加入alert()就成功执行,不加就不对的问题!

    问题: jquery中的$(document).ready(function(){})中调用两个方法(1)利用ajax请求去后台查图书类别的方法(2)当页面上利用图书类别去查询图书返回页面,让图书类别 ...

  9. Linux环境下搭建Git仓库

    1.安装Git $ yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-devel $ yum ...

  10. open和close函数

    1.open函数的使用 调用open函数可以打开或创建一个文件 #include <sys/stat.h> #include <fcntl.h> #include <sy ...