我正在尝试获取此URL“ http://www.example.com/womens-collection/ceramic”以重定向到URL“ http://www.example.com/the-ceramic”。下面是代码

   if(window.location.pathname == "/womens-collection/ceramic") {
   //alert("worked");
   document.location = "/the-ceramic";
   //$('#jbw-spot-mobile_details').toggleClass('opened')}


效果很好,但我不希望其他任何受产品影响的URL受到此影响,例如“ http://www.example.com/womens-collection/ceramic?page=shop.product_details”

我怎样才能让jquery只识别“ / womens-collection / ceramic”,之后什么也不能识别?

最佳答案

regex = /(/womens-collection/ceramic)$/i; // Using the i flag for case insensitivity. Feel free to remove if you want.
path = window.location.pathname;
if (path.match(regex)) {
   // Do stuff.
}


只需使用正则表达式在路径末尾进行匹配即可。

09-11 07:05