Javascript匹配url的一部分,if语句基于结果

2024-02-19

这是我尝试匹配的网址示例:http://store.mywebsite.com/folder-1/folder-2/item3423434.aspx http://store.mywebsite.com/folder-1/folder-2/item3423434.aspx

我试图匹配的是http://store.mywebsite.com/folder-1,但“folder-1”始终是不同的值。我不知道如何为此编写 if 语句:

示例(伪代码)

if(url contains http://store.mywebsite.com/folder-1)
do this

else if (url contains http://store.mywebsite.com/folder-2)
do something else

etc


为了让事情变得非常简单......

if(location.pathname.indexOf("folder-1") != -1)
{
    //do things for "folder-1"
}

如果值“folder-1”可能出现在字符串的其他部分,这可能会给您带来误报。如果您已经确定情况并非如此,那么提供的示例应该足够了。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Javascript匹配url的一部分,if语句基于结果 的相关文章

随机推荐