【问题标题】:Create RegEx control for URL为 URL 创建 RegEx 控件
【发布时间】:2021-03-21 09:26:58
【问题描述】:

我当前的 Web 应用程序有问题。确实,当我放置当前链接时:https://localhost:44311/#shop,我的页面显示完美。这是一张图片:

但是当我尝试将我的 URL 更改为: https://localhost:44311/#/ ,以验证重定向的控制时,显示的内容变成了相同的内容,这里是发生了什么的图片:

目前,我所有的 ajax 调用都被一次又一次地调用(无限循环)。

我正在尝试为#/ 添加控制正则表达式,并尝试尊重#/xxxx/xxxxx。但没有成功。如果你能帮助我,那就太好了! 这是我管理加载页面的 JS:

var ui = {
   controller: {
       page: {}
   },
   component: {
       category: null
   }
};
ui.controller.pageLoad = function (hash = null) {
   if (hash === null) {
       hash = window.location.hash;
       if (hash.length < 2 || hash[0] !== "#") {
           hash = "/shop";
       }
   } else if (hash.length < 2 || hash[0] === "#" && hash[1] === '/') { // add control regex for #/, must respect #/xxxx/xxxxx
       hash = "/shop";
   }

   $.get(hash.substring(1), function (data) {
       ui.controller.page = null;
       $("#content").html(data);
   }).fail(function (error) {
       $.get("/Home/Error?status=" + error.status, function (data) {
           ui.controller.page = null;
           $("#content").html(data);
       });
   });
};

【问题讨论】:

    标签: javascript c# regex asp.net-mvc vue.js


    【解决方案1】:

    您可以创建一个isValidHash 函数,并在您的if 语句中使用它:

    function isValidHash(hash) {
      return typeof hash === "string" && /^#(\/[a-z0-9-]+)+$/.test(hash);
    }
    
    function test(v) {
      console.log(`'${v}' is ${isValidHash(v) ? '' : 'NOT '}valid`);
    }
    
    test(null);            // NO - not a string
    test('#');             // NO - too short
    test('#shop');         // NO - no slash
    test('#/shop');        // YES
    test('#/shop/item-9'); // YES

    【讨论】:

    • 哦,是的,谢谢它的工作原理。先生,您有一些关于正则表达式的链接可以与我分享吗?我还是个初学者啊啊啊。再次感谢!
    • 当然,您可以go here,查看该正则表达式的实际作用,在右侧您将找到该正则表达式中所有内容的解释
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-05
    • 1970-01-01
    • 2018-09-14
    • 2012-09-30
    • 2013-01-29
    • 1970-01-01
    相关资源
    最近更新 更多