理解javascript拆分有点帮助

时间:2014-05-29 13:18:08

标签: javascript

我基本上是在尝试调试一些javascript,我真的不明白它。

这是代码

if(a&&0<=a.indexOf("://")&&a.split("/")[2]!=f.location.href.split("/")[2])

这是值

A = "/cc/subs/TOS-turkish.srt"

f.location - "http://192.168.55.108:5555/cc/mypage.html"

这可能是他们认为匹配的每个人的情况。我现在真的很困惑。

感谢。

4 个答案:

答案 0 :(得分:1)

调用split将内容拆分为数组。在split("/")上拨打"http://192.168.55.108:5555/cc/mypage.html"会给您:

["http:", "", "192.168.55.108:5555", "cc", "mypage.html"]

[2]的{​​{1}}索引为"192.168.55.108:5555"

if语句的作用是检查它们是否 匹配:

a.split("/")[2] != f.location.href.split("/")[2]
                ^

如果if不等于a.split("/")[2]"192.168.55.108:5555"语句的这一部分将会成功。

split("/")上拨打"/cc/subs/TOS-turkish.srt"会给您:

["", "cc", "subs", "TOS-turkish.srt"]

因此,if语句的这一部分会成功,因为"subs"(上述数组的[2]索引)不等于"192.168.55.108:5555"

然而 if语句在此之前将失败,因为a没有"://",因此a.indexOf("://")会返回-1和{ {1}}不小于或等于0


完全分解-1声明:

if

由于它在 a // true: a = "/cc/subs/TOS-turkish.srt" && 0 <= a.indexOf("://") // false: 0 is greater than -1 && ...[2] != ...[2] // true: "subs" isn't equal to "192.168.55.108:5555" 上返回false,因此无论如何都不会到达最后一部分。

答案 1 :(得分:0)

JavaScript .split()方法允许您通过提供要分割字符串的关键字(或字符)或正则表达式将字符串拆分为数组。

e.g。在你的情况下,它正在分裂角色&#34; /&#34;。

完成拆分后,您可以通过索引直接访问数组中的项...所以[2]获取数组中的第3项(因为数组是零索引的)

假设您希望将此序列号中的数字块放入数组中:

var sn = '123-456-789';
var chunks = sn.split('-');
//chunks is now an array of: ['123', '456', '789']

答案 2 :(得分:0)

此比较尝试查看输入网址的域是否与当前页面的域匹配。

"http://stackoverflow.com".split("/")[2]提供stackoverflow.com


这段代码可能是一个脚本的一部分,它会查看页面中的链接,如果它们指向异地资源就会对它们执行某些操作,比如说:“嘿,你要离开这个网站了,你确定要吗?继续?”。

如果您显示更多代码,我可能会给出更明确的答案。

答案 3 :(得分:0)

此评估为false,因为:

if (
    a && // <-- if a is true
    0 <= a.indexOf("://") && // <-- if there is '://' in a
    a.split("/")[2] != f.location.href.split("/")[2] // <-- the 2nd part of a, split by '/' (in your case 'subs') is not equal to the 2nd part of f, split by '/' (in your case '192.168.55.108:5555')
) { 
    do_something();
}