javascript 无法读取未定义的属性“匹配”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28192958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 08:33:32  来源:igfitidea点击:

Cannot read property 'match' of undefined

javascriptjquery

提问by rob.m

I'm running the following:

我正在运行以下内容:

var token = document.location.href.split('?s=')[1].match(/[a-z0-9]+/);
var longString = "?s=" + token + "?_sft_category=";
var tokenB = document.location.href.split(longString)[1].match(/[a-z0-9]+/);
var attribuB = "." + tokenB;
jQuery('a[data-filter-value="' + attribuB + '"]').parent().parent().parent().find(".dropdown-toggle").html(tokenB).append(' <span class="caret"></span>');

How come I get?

我怎么得到?

Uncaught TypeError: Cannot read property 'match' of undefined

If I remove .match(/[a-z0-9]+/)i don't get any error, but I do need match..

如果我删除.match(/[a-z0-9]+/)我不会收到任何错误,但我确实需要match..

The URL looks like:

该网址如下所示:

http://www.example.com/xchanges/results/?s=sky&_sft_category=ogilvy

采纳答案by Cyclonecode

You are using ?twice in longString, which in turn will make split()return an array with only one value. You need to change it to:

?在 in中使用了两次longString,这反过来将split()返回一个只有一个值的数组。您需要将其更改为:

var longString = "?s=" + token + "&_sft_category=";

The ?character should only be used once, at the start of the querystring.

?字符只能在查询字符串的开头使用一次。

回答by Ionic? Biz?u

Probably using a library would be better here. I developed a tiny JavaScript library that work with the urls: url.js.

可能在这里使用图书馆会更好。我开发了一个使用 urls 的小型 JavaScript 库:url.js.

<script src="path/to/url.js"></script>
<script src="your-js.js"></script>

Then you can do:

然后你可以这样做:

var token = Url.queryString("s");
var tokenB = Url.queryString("_sft_category");

And that's it.

就是这样。

Note that instead of calling parent()multiple times, you can use the closest()method:

请注意parent(),您可以使用该closest()方法,而不是多次调用:

$('a[data-filter-value="' + attribuB + '"]')
   .closest("<your_container>")
   .find(".dropdown-toggle")
   .html(tokenB)
   .append(' <span class="caret"></span>')
   ;

回答by Reinder Wit

Your location.href probably doesn't contain your longString. If you split the hrefusing a string that's not present in href, you're left with an array with only 1 string, so you cannot get the [1] element. It all depends on what your tokenvariable is....

您的 location.href 可能不包含您的longString. 如果您href使用 中不存在的字符串拆分href,则会留下一个只有 1 个字符串的数组,因此您无法获得 [1] 元素。这一切都取决于你的token变量是什么......

You've used a '?' character twice, which is incorrect. I'd use

你用过'?' 字符两次,这是不正确的。我会用

var tokenB = document.location.href.split('_sft_category=')[1].match(/[a-z0-9]+/);

to get the desired value, as _sft_category=will probably only occur once in location.href and you should then be able to split the href into an array of 2. Saves you 2 lines of code ;) ...

以获得所需的值,因为_sft_category=可能只会在 location.href 中出现一次,然后您应该能够将 href 拆分为 2 的数组。为您节省 2 行代码;) ...