简体   繁体   English

需要帮助说明javascript正则表达式

[英]Need help on explaning javascript regular expression

I just found this code snippet that has to do with filtering hash tag but I don't understand what is it trying to do, and I'm not sure what to google either. 我刚刚发现该代码段与过滤哈希标签有关,但我不知道它要做什么,而且我也不知道要用谷歌搜索什么。

Thanks for the heads up... 感谢您的注意...

var index = 0;
var hash = window.location.hash; //store the entered hash value eg, #02

if (hash) {
    index = /\d+/.exec(hash)[0];
    index = (parseInt(index) || 1) - 1;
}
  • if (hash) { : If hash is not a garbage value like undefined , null or empty string. if (hash) { :如果hash 不是undefinednull或空字符串之类的垃圾值。
  • index = /\\d+/.exec(hash)[0] : Look for the first number inside the hash, for example inside #432 that would be 432 (Note that the returned value is a string) . index = /\\d+/.exec(hash)[0] :在哈希中查找第一个数字,例如在#432内部查找为432 (请注意,返回值是一个字符串)
  • index = (parseInt(index) || 1) - 1 : Try to convert index to a number, if that worked out and the resulting number is not 0 then subtract 1 from the returned value, otherwise return 1 and then subtract 1 from it thus giving us 0 , the main idea here is that it seems we are trying to get an index to an array so the index can't be less than 0 . index = (parseInt(index) || 1) - 1 :尝试将index转换为数字,如果该数字可行且结果数字不是0则从返回值中减去1 ,否则返回1 ,然后从中减去1因此给我们0 ,这里的主要想法是似乎我们试图获取一个数组的索引,这样索引不能小于0

/\\d+/ means one or more digits. /\\d+/表示一个或多个数字。

+ means one or more of the preceding element. +表示一个或多个前面的元素。

Also exec 执行

If the match succeeds, the exec method returns an array and updates properties of the regular expression object. 如果匹配成功,则exec方法返回一个数组并更新正则表达式对象的属性。 The returned array has the matched text as the first item, and then one item for each capturing parenthesis that matched containing the text that was captured. 返回的数组具有匹配的文本作为第一项,然后是每个与捕获的括号匹配的项,其中包含捕获的文本。

If the match fails, the exec method returns null. 如果匹配失败,则exec方法返回null。

In a regular expression \\d will match a single number. 在正则表达式中\\d将匹配单个数字。 The + in the regex will match repeats of the expression before it. 正则表达式中的+将匹配表达式之前的重复项。 So \\d+ will match a full (all repetive) number. 因此\\d+将匹配一个完整的(所有重复的)数字。

So 所以

"55".match(/\d+/) //=>["55"]
"55".match(/\d/) //=>["5"]
"A string with 55".match(/\d+/) //=>["55"]
var hash = window.location.hash; //store the entered hash value eg, #02

Just gets the # part of the URL 只是获取URL的#部分

if (hash) {

Checks if the hash is not empty 检查哈希是否不为空

    index = /\d+/.exec(hash)[0];

Tries to match the hash against a sequence of digits (that's what \\d+ means) and assigns the first such sequence to index 尝试将散列与数字序列匹配(这就是\\ d +的含义),并将第一个这样的序列分配给index

    index = (parseInt(index) || 1) - 1;

Just converts index to a number and decreases it by 1 只需将索引转换为数字并将其减少1

If the hash contains no digits, this code throws an exception, so it's not very robust 如果散列不包含数字,则此代码将引发异常,因此它不是很健壮

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

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