[英]How to obtain URL parameter using jquery in case insensitive way
Is there a way to obtain a URL parameter in a case insensitive way using jquery?有没有办法使用 jquery 以不区分大小写的方式获取 URL 参数?
Essentially, I'm looking to do something like $.url('?someparameter');
本质上,我希望做一些类似
$.url('?someparameter');
, where it would match both http:\\\\www.test.com?someparameter=ABC
or ,它将匹配
http:\\\\www.test.com?someparameter=ABC
或http:\\\\www.test.com?SOMEparAMeter=ABC
You should try toLowerCase .你应该尝试toLowerCase 。 This function converts any string to lowercase.
此函数将任何字符串转换为小写。
Use a regular expression where you set the case-insensitive flag.使用正则表达式来设置不区分大小写的标志。
Regular Expressions -- scroll down to "Advanced Searching With Flags" 正则表达式——向下滚动到“使用标志进行高级搜索”
Please take a look at: How can I get query string values in JavaScript?请看一下: 如何在 JavaScript 中获取查询字符串值?
The line to adapt to your needs is as follows:适应您需求的线路如下:
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)", "i");
//"i" for case-insensitive
This doesnt use jQuery, just javascript.这不使用 jQuery,只是 javascript。 But it addresses the question in general.
但它解决了一般问题。
The problem w/ ucasing the entire ULR is you may be keying off the value to look up an HTML element.使用整个 ULR 的问题是您可能会关闭值以查找 HTML 元素。
why there is not a collection of keys in URL.searchParams, I do not know, but there is not.为什么 URL.searchParams 中没有键的集合,我不知道,但没有。 Below is a function i wrote that will find a key and return a value.
下面是我写的一个函数,它将找到一个键并返回一个值。
I am just barely literate in regEx, so I am sure there is a better regEx that can pull the value out and omit trailing key value pairs.我对 regEx 几乎不了解,所以我确信有一个更好的 regEx 可以提取值并省略尾随键值对。
function getParm_CI(parm) {
var str = window.location.href;
var rgx = new RegExp('\\b' + parm + '=.*\\b', 'gi');
//this gets an array of matches
var aMatches = str.match(rgx);
if (aMatches == null) return;
var parmVal = aMatches[0].substring(parm.length + 1);
//we shouldnt, but make sure there are not trailing parms
var idx = parmVal.indexOf('&');
//alert('amp:' + idx);
if (idx > -1) parmVal = parmVal.substring(0, idx);
return parmVal;
}
usage would be like this用法是这样的
var topic = getParm_CI('SOMEparAMeter');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.