这个javascript正则表达式做了什么?:“var match = self.location.href.replace(/ \ / $ / i,'');”

时间:2021-04-06 01:58:35

I saw this expression in another codebases library as part of the following sequence:

我在另一个代码库中看到了这个表达式,作为以下序列的一部分:

var url = sel.anchorNode.parentNode.href;
var match = self.location.href.replace(/\/$/i, '');
var replaced = url.replace(match,'');

It was suggest that the regular expression might strip of the trailing path to just reutrn the base URL but I created a fiddle to test that theory and it doesn't seem to check out.

有人建议正则表达式可能会删除尾随路径以仅仅重新定位基本URL,但我创建了一个小提琴来测试该理论并且它似乎没有检查出来。

http://jsfiddle.net/funkyeah/WEQZZ/

http://jsfiddle.net/funkyeah/WEQZZ/

2 个解决方案

#1


3  

It only strips the trailing slash. A single slash /

它只删除尾部斜杠。单斜杠/

#2


0  

The regular expression strips a trailing slash (if any), but that in combination with the last line (replaced = ...) gets the URL of the link with the URL of the current page (with the trailing slash, if any, removed) removed from it.

正则表达式删除一个尾部斜杠(如果有的话),但它与最后一行(被替换= ...)结合使用当前页面的URL获取链接的URL(如果有的话,删除尾部斜杠)从中删除。

#1


3  

It only strips the trailing slash. A single slash /

它只删除尾部斜杠。单斜杠/

#2


0  

The regular expression strips a trailing slash (if any), but that in combination with the last line (replaced = ...) gets the URL of the link with the URL of the current page (with the trailing slash, if any, removed) removed from it.

正则表达式删除一个尾部斜杠(如果有的话),但它与最后一行(被替换= ...)结合使用当前页面的URL获取链接的URL(如果有的话,删除尾部斜杠)从中删除。