繁体   English   中英

jQuery.grep()匹配www和非www,http / https的URL

[英]jQuery.grep() match URL both www and non www, http/https

我正在使用jquery搜索匹配的URL的json字符串,然后获取该对象内的所有数据。

我用

var url = window.location.href;

但是,这返回

http://somesite.com/

json字符串中可能是以下任何内容

等等等

我的代码是匹配url,然后执行某些操作。 我将如何使用jQuery.grep()在查询中采用不同的样式网址? 下面是到目前为止的代码。

var url = window.location.href;
var json = exampleJsonString;
var js = JSON.parse(json);
var result = $.grep(js, function(e){ return e.url == url; });

if (result.length == 0) {
  // not found :(
  console.log('Not found');
} else if (result.length == 1) {
  // Result matched
  console.log(result);

} 

else {
  // multiple items found
  console.log('multiple items found');
  console.log(result); 
}

我想做的是使用jquery grep检查somesite.com。 如该行所示,它检查字符串是否相等。

var result = $.grep(js, function(e){ return e.url == url; }); 

您可以使用grep函数来过滤记录。 在这里,您要检查对象中的不同URL,例如“ http://somesite.com/ ”,“ http://somesite.com ”等,并提及您要在grep函数中考虑的所有URL。 这是示例代码。

   var varObj = jQuery.grep(dataobject, function (a) {
            if (a.url == "http://somesite.com/" || a.url == "http://somesite.com" || a.url == "http://www.somesite.com/")
                return true;
        });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM