从网址中删除所有空值 [英] remove all empty values from url

查看:92
本文介绍了从网址中删除所有空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从网址中删除所有空值:

I want to remove all empty values from an url:

var s="value1=a&value2=&value3=b&value4=c&value5=";
s = s.replace(...???...);
alert(s);

预期产出:

value1=a&value3=b&value4=c

我只需要查询要考虑的URL的一部分。

I only need the query part of the URL to be taken into account.

推荐答案

这样的事情:

s = s.replace(/[^=&]+=(&|$)/g,"").replace(/&$/,"");

也就是说,删除一个或多个非等号/非符号字符的组,后跟等号和符号或字符串结尾。然后删除任何剩余的尾随&符号。

That is, remove groups of one or more non-equals/non-ampersand characters that are followed by an equals sign and ampersand or end of string. Then remove any leftover trailing ampersand.

演示: http://jsfiddle.net/pKHzr/

这篇关于从网址中删除所有空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆