javascript regex - 删除查询字符串变量(如果存在) [英] javascript regex - remove a querystring variable if present
问题描述
我需要使用 javascript 重写一个查询.首先,我检查变量是否存在,如果存在,我想用新的搜索词替换它.如果它不存在,那么我只需添加新变量
I need to rewrite a querysting using javascript. First I check to see if the variable is present, if it is I want to replace it with a new search term. If it is not present then I just add the new variable
我可以使用诸如 hat、ufc hat 之类的简单术语来使用它空格是 %20,所以 ufc hat 真的是 ufc%20hat
I'm able to get it to work with simple terms like hat, ufc hat whitespaces are %20, so ufc hat is really ufc%20hat
我遇到了化妆、帽子棒球、咖啡和等术语的问题.茶等.
I run into problem with terms like make-up, hat -baseball, coffee & tea, etc..
什么是合适的正则表达式?
What is the proper regex for this?
下面是我的代码,它不起作用.
Below is my code, which doesn't work.
var url = String(document.location).split('?');
querystring = url[1];
if(querystring.match(/gbn_keywords=/)!=null)
querystring=querystring.replace(/gbn_keywords=[a-zA-Z0-9%20.]+/,"gbn_keywords="+term);
else
querystring=querystring+"&gbn_keywords="+term;
推荐答案
location
对象已经拥有非常好的属性,例如 pathname
、hostname
等. 为您提供 URL 的单独部分.使用 .search
属性,而不是尝试将 URL 作为字符串破解(?
可能不仅出现在那个地方).
location
objects already have perfectly good properties like pathname
, hostname
etc. that give you the separate parts of a URL. Use the .search
property instead of trying to hack the URL as a string (?
may not only appear in that one place).
然后是在 &
字符上拆分的情况(如果您想保持友好,也可能是 ;
,根据 HTML4 B2.2)并检查每个与您要查找的参数相对应.对于一般情况,这需要正确的 URL 解码,因为 g%62n_keywords=...
是拼写相同参数的有效方式.自然而然地,您将需要再次编码,以阻止 &
继续处理下一个参数(以及包含其他无效字符).
It's then a case of splitting on the &
character (and maybe ;
too if you want to be nice, as per HTML4 B2.2) and checking each parameter against the one you're looking for. For the general case this requires proper URL-decoding, as g%62n_keywords=...
is a valid way of spelling the same parameter. On the way out naturally you will need to encode again, to stop &
going on to the next parameter (as well as to include other invalid characters).
这里有几个实用函数,您可以使用它们来更轻松地处理查询字符串操作.它们在 location.search
或 link.search
中看到的 ?...
字符串和查找 Object
之间进行转换> 将参数名称映射到值数组(因为表单 url 编码的查询可以有同一参数的多个实例).
Here's a couple of utility functions you can use to cope with query string manipulation more easily. They convert between the ?...
string as seen in location.search
or link.search
and a lookup Object
mapping parameter names to arrays of values (since form-url-encoded queries can have multiple instances of the same parameter).
function queryToLookup(query) {
var lookup= {};
var params= query.slice(1).split(/[&;]/);
for (var i= 0; i<params.length; i++) {
var ix= params[i].indexOf('=');
if (ix!==-1) {
var name= decodeURIComponent(params[i].slice(0, ix));
var value= decodeURIComponent(params[i].slice(ix+1));
if (!(name in lookup))
lookup[name]= [];
lookup[name].push(value);
}
}
return lookup;
}
function lookupToQuery(lookup) {
var params= [];
for (var name in lookup)
for (var i= 0; i<lookup[name].length; i++)
params.push(encodeURIComponent(name)+'='+encodeURIComponent(lookup[name][i]));
return params.length===0? '' : '?'+params.join('&');
}
这使得使用变得简单:
var lookup= queryToLookup(location.search);
lookup['gbn_keywords']= ['coffee & tea'];
var query= lookupToQuery(lookup);
这篇关于javascript regex - 删除查询字符串变量(如果存在)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!