---
title: "[正则]改变URL中的参数值"
author: "zccst"
date: "2015-05-20T20:37:49Z"
tags: ["JavaScript"]
categories: ["Web前端"]
slug: "url"
draft: false
lastmod: "2015-05-20T20:49:13Z"
id: 2212989
url: "https://zccst.iteye.com/blog/2212989"
---

作者：zccst

如果一个URL是：http://www.example.com/index.html?id=100&name=xx&age=20

希望将name=meinv怎么办？

```js

function getQueryString(name) {
	var reg = new RegExp("(\\?|^|&|\#)" + name + "=([^&|^#]*)(&|$|#)", "i");
	var r = window.location.search.substr(1).match(reg);
	if (r != null) return unescape(r[2]); return null;
}

```



```js

var url = 'http://www.example.com/index.html?id=100&name=xx&age=20';
function test(key, value){
	var u = url.replace(/(\\?|^|&|\#)name=([^&|^#]*)(&|$|#)/, "$1"+key+"="+value+"$2"); 
	return u;
}
var r = test('name', 'meinv');console.log(r);
VM792:6 http://www.example.com/index.html?id=100&name=meinvxxage=20

```


参考：<http://www.jb51.net/article/43949.htm>