preface

In front-end single-page applications, routing is an important part, the existing routing system from simple director. Js to backbone, React and other built-in routes, the function of step by step enhanced. So what are these system principles? This article will analyze and implement a simple routing to explain how it works.

Simple routing implementation

Using hash as an example, its operation mechanism is as follows:

  • Stores hash and corresponding callback functions in the form of keys and values in the object cache

  • Set the listener function onHashChange to listen for hash changes in urls

  • If the hash changes, the cache object is traversed, the key attribute is regedded, the corresponding re is generated, the re is matched with the hash, and the corresponding value/ callback function is executed

  • The code that renders the UI is executed in the callback function to update the page

router.js

    function Router() {
    this.cache = {};
    //将url/callback 以key/value形式储存在cache内
    this.on = function (key, value) {
        var cache = this.cache;
        cache[key] = value;
    };
    //匹配hash对应的回调函数,并触发
    this.trigger = function (hash) {
        var cache = this.cache;
        for (var r in cache) {
            var reg = this.initRegexps(r);
            if (reg.test(hash)) {
                var callback = cache[r] || function () {
                    };
                var params = this.getParams(reg, hash);
                callback.apply(this, params);
            }
        }

    };
    //初始化 添加监听浏览器hashchange 以及dom loaded函数
    this.init = function () {
        window.addEventListener('hashchange', function () {
            var hash = location.hash.slice(1);
            router.trigger(hash);
        });
        window.addEventListener('load', function () {
            var hash = location.hash.slice(1) || 'default';
            router.trigger(hash);
        })
    };
    /**
     *将cache内的key 做正则处理,并返回
     * 第一个正则 匹配诸如/,.+-?$#{}[]] 关键字  并在关键字前面加转译字符\
     * 第二个正则 匹配() 标示()内部内容可有可无
     * 第三个正则 匹配: 在/后面可以由接受任意字符,直到遇到下一个/
     * 第四个正则 匹配* 在*后面可以由接受任意字符
     */
    this.initRegexps = function (route) {
        route = route.replace(/[/,.+\-?$#{}\[\]]/g, '\\    function Router() {
    this.cache = {};
    //将url/callback 以key/value形式储存在cache内
    this.on = function (key, value) {
        var cache = this.cache;
        cache[key] = value;
    };
    //匹配hash对应的回调函数,并触发
    this.trigger = function (hash) {
        var cache = this.cache;
        for (var r in cache) {
            var reg = this.initRegexps(r);
            if (reg.test(hash)) {
                var callback = cache[r] || function () {
                    };
                var params = this.getParams(reg, hash);
                callback.apply(this, params);
            }
        }

    };
    //初始化 添加监听浏览器hashchange 以及dom loaded函数
    this.init = function () {
        window.addEventListener('hashchange', function () {
            var hash = location.hash.slice(1);
            router.trigger(hash);
        });
        window.addEventListener('load', function () {
            var hash = location.hash.slice(1) || 'default';
            router.trigger(hash);
        })
    };
    /**
     *将cache内的key 做正则处理,并返回
     * 第一个正则 匹配诸如/,.+-?$#{}[]] 关键字  并在关键字前面加转译字符\
     * 第二个正则 匹配() 标示()内部内容可有可无
     * 第三个正则 匹配: 在/后面可以由接受任意字符,直到遇到下一个/
     * 第四个正则 匹配* 在*后面可以由接受任意字符
     */
    this.initRegexps = function (route) {
        route = route.replace(/[/,.+\-?$#{}\[\]]/g, '\\    function Router() {
    this.cache = {};
    //将url/callback 以key/value形式储存在cache内
    this.on = function (key, value) {
        var cache = this.cache;
        cache[key] = value;
    };
    //匹配hash对应的回调函数,并触发
    this.trigger = function (hash) {
        var cache = this.cache;
        for (var r in cache) {
            var reg = this.initRegexps(r);
            if (reg.test(hash)) {
                var callback = cache[r] || function () {
                    };
                var params = this.getParams(reg, hash);
                callback.apply(this, params);
            }
        }

    };
    //初始化 添加监听浏览器hashchange 以及dom loaded函数
    this.init = function () {
        window.addEventListener('hashchange', function () {
            var hash = location.hash.slice(1);
            router.trigger(hash);
        });
        window.addEventListener('load', function () {
            var hash = location.hash.slice(1) || 'default';
            router.trigger(hash);
        })
    };
    /**
     *将cache内的key 做正则处理,并返回
     * 第一个正则 匹配诸如/,.+-?$#{}[]] 关键字  并在关键字前面加转译字符\
     * 第二个正则 匹配() 标示()内部内容可有可无
     * 第三个正则 匹配: 在/后面可以由接受任意字符,直到遇到下一个/
     * 第四个正则 匹配* 在*后面可以由接受任意字符
     */
    this.initRegexps = function (route) {
        route = route.replace(/[/,.+\-?$#{}\[\]]/g, '\\    function Router() {
    this.cache = {};
    //将url/callback 以key/value形式储存在cache内
    this.on = function (key, value) {
        var cache = this.cache;
        cache[key] = value;
    };
    //匹配hash对应的回调函数,并触发
    this.trigger = function (hash) {
        var cache = this.cache;
        for (var r in cache) {
            var reg = this.initRegexps(r);
            if (reg.test(hash)) {
                var callback = cache[r] || function () {
                    };
                var params = this.getParams(reg, hash);
                callback.apply(this, params);
            }
        }

    };
    //初始化 添加监听浏览器hashchange 以及dom loaded函数
    this.init = function () {
        window.addEventListener('hashchange', function () {
            var hash = location.hash.slice(1);
            router.trigger(hash);
        });
        window.addEventListener('load', function () {
            var hash = location.hash.slice(1) || 'default';
            router.trigger(hash);
        })
    };
    /**
     *将cache内的key 做正则处理,并返回
     * 第一个正则 匹配诸如/,.+-?$#{}[]] 关键字  并在关键字前面加转译字符\
     * 第二个正则 匹配() 标示()内部内容可有可无
     * 第三个正则 匹配: 在/后面可以由接受任意字符,直到遇到下一个/
     * 第四个正则 匹配* 在*后面可以由接受任意字符
     */
    this.initRegexps = function (route) {
        route = route.replace(/[/,.+\-?$#{}\[\]]/g, '\\    function Router() {
    this.cache = {};
    //将url/callback 以key/value形式储存在cache内
    this.on = function (key, value) {
        var cache = this.cache;
        cache[key] = value;
    };
    //匹配hash对应的回调函数,并触发
    this.trigger = function (hash) {
        var cache = this.cache;
        for (var r in cache) {
            var reg = this.initRegexps(r);
            if (reg.test(hash)) {
                var callback = cache[r] || function () {
                    };
                var params = this.getParams(reg, hash);
                callback.apply(this, params);
            }
        }

    };
    //初始化 添加监听浏览器hashchange 以及dom loaded函数
    this.init = function () {
        window.addEventListener('hashchange', function () {
            var hash = location.hash.slice(1);
            router.trigger(hash);
        });
        window.addEventListener('load', function () {
            var hash = location.hash.slice(1) || 'default';
            router.trigger(hash);
        })
    };
    /**
     *将cache内的key 做正则处理,并返回
     * 第一个正则 匹配诸如/,.+-?$#{}[]] 关键字  并在关键字前面加转译字符\
     * 第二个正则 匹配() 标示()内部内容可有可无
     * 第三个正则 匹配: 在/后面可以由接受任意字符,直到遇到下一个/
     * 第四个正则 匹配* 在*后面可以由接受任意字符
     */
    this.initRegexps = function (route) {
        route = route.replace(/[/,.+\-?$#{}\[\]]/g, '\\    function Router() {
    this.cache = {};
    //将url/callback 以key/value形式储存在cache内
    this.on = function (key, value) {
        var cache = this.cache;
        cache[key] = value;
    };
    //匹配hash对应的回调函数,并触发
    this.trigger = function (hash) {
        var cache = this.cache;
        for (var r in cache) {
            var reg = this.initRegexps(r);
            if (reg.test(hash)) {
                var callback = cache[r] || function () {
                    };
                var params = this.getParams(reg, hash);
                callback.apply(this, params);
            }
        }

    };
    //初始化 添加监听浏览器hashchange 以及dom loaded函数
    this.init = function () {
        window.addEventListener('hashchange', function () {
            var hash = location.hash.slice(1);
            router.trigger(hash);
        });
        window.addEventListener('load', function () {
            var hash = location.hash.slice(1) || 'default';
            router.trigger(hash);
        })
    };
    /**
     *将cache内的key 做正则处理,并返回
     * 第一个正则 匹配诸如/,.+-?$#{}[]] 关键字  并在关键字前面加转译字符\
     * 第二个正则 匹配() 标示()内部内容可有可无
     * 第三个正则 匹配: 在/后面可以由接受任意字符,直到遇到下一个/
     * 第四个正则 匹配* 在*后面可以由接受任意字符
     */
    this.initRegexps = function (route) {
        route = route.replace(/[/,.+\-?$#{}\[\]]/g, '\\    function Router() {
    this.cache = {};
    //将url/callback 以key/value形式储存在cache内
    this.on = function (key, value) {
        var cache = this.cache;
        cache[key] = value;
    };
    //匹配hash对应的回调函数,并触发
    this.trigger = function (hash) {
        var cache = this.cache;
        for (var r in cache) {
            var reg = this.initRegexps(r);
            if (reg.test(hash)) {
                var callback = cache[r] || function () {
                    };
                var params = this.getParams(reg, hash);
                callback.apply(this, params);
            }
        }

    };
    //初始化 添加监听浏览器hashchange 以及dom loaded函数
    this.init = function () {
        window.addEventListener('hashchange', function () {
            var hash = location.hash.slice(1);
            router.trigger(hash);
        });
        window.addEventListener('load', function () {
            var hash = location.hash.slice(1) || 'default';
            router.trigger(hash);
        })
    };
    /**
     *将cache内的key 做正则处理,并返回
     * 第一个正则 匹配诸如/,.+-?$#{}[]] 关键字  并在关键字前面加转译字符\
     * 第二个正则 匹配() 标示()内部内容可有可无
     * 第三个正则 匹配: 在/后面可以由接受任意字符,直到遇到下一个/
     * 第四个正则 匹配* 在*后面可以由接受任意字符
     */
    this.initRegexps = function (route) {
        route = route.replace(/[/,.+\-?$#{}\[\]]/g, '\\    function Router() {
    this.cache = {};
    //将url/callback 以key/value形式储存在cache内
    this.on = function (key, value) {
        var cache = this.cache;
        cache[key] = value;
    };
    //匹配hash对应的回调函数,并触发
    this.trigger = function (hash) {
        var cache = this.cache;
        for (var r in cache) {
            var reg = this.initRegexps(r);
            if (reg.test(hash)) {
                var callback = cache[r] || function () {
                    };
                var params = this.getParams(reg, hash);
                callback.apply(this, params);
            }
        }

    };
    //初始化 添加监听浏览器hashchange 以及dom loaded函数
    this.init = function () {
        window.addEventListener('hashchange', function () {
            var hash = location.hash.slice(1);
            router.trigger(hash);
        });
        window.addEventListener('load', function () {
            var hash = location.hash.slice(1) || 'default';
            router.trigger(hash);
        })
    };
    /**
     *将cache内的key 做正则处理,并返回
     * 第一个正则 匹配诸如/,.+-?$#{}[]] 关键字  并在关键字前面加转译字符\
     * 第二个正则 匹配() 标示()内部内容可有可无
     * 第三个正则 匹配: 在/后面可以由接受任意字符,直到遇到下一个/
     * 第四个正则 匹配* 在*后面可以由接受任意字符
     */
    this.initRegexps = function (route) {
        route = route.replace(/[/,.+\-?$#{}\[\]]/g, '\\    function Router() {
    this.cache = {};
    //将url/callback 以key/value形式储存在cache内
    this.on = function (key, value) {
        var cache = this.cache;
        cache[key] = value;
    };
    //匹配hash对应的回调函数,并触发
    this.trigger = function (hash) {
        var cache = this.cache;
        for (var r in cache) {
            var reg = this.initRegexps(r);
            if (reg.test(hash)) {
                var callback = cache[r] || function () {
                    };
                var params = this.getParams(reg, hash);
                callback.apply(this, params);
            }
        }

    };
    //初始化 添加监听浏览器hashchange 以及dom loaded函数
    this.init = function () {
        window.addEventListener('hashchange', function () {
            var hash = location.hash.slice(1);
            router.trigger(hash);
        });
        window.addEventListener('load', function () {
            var hash = location.hash.slice(1) || 'default';
            router.trigger(hash);
        })
    };
    /**
     *将cache内的key 做正则处理,并返回
     * 第一个正则 匹配诸如/,.+-?$#{}[]] 关键字  并在关键字前面加转译字符\
     * 第二个正则 匹配() 标示()内部内容可有可无
     * 第三个正则 匹配: 在/后面可以由接受任意字符,直到遇到下一个/
     * 第四个正则 匹配* 在*后面可以由接受任意字符
     */
    this.initRegexps = function (route) {
        route = route.replace(/[/,.+\-?$#{}\[\]]/g, '\\    function Router() {
    this.cache = {};
    //将url/callback 以key/value形式储存在cache内
    this.on = function (key, value) {
        var cache = this.cache;
        cache[key] = value;
    };
    //匹配hash对应的回调函数,并触发
    this.trigger = function (hash) {
        var cache = this.cache;
        for (var r in cache) {
            var reg = this.initRegexps(r);
            if (reg.test(hash)) {
                var callback = cache[r] || function () {
                    };
                var params = this.getParams(reg, hash);
                callback.apply(this, params);
            }
        }

    };
    //初始化 添加监听浏览器hashchange 以及dom loaded函数
    this.init = function () {
        window.addEventListener('hashchange', function () {
            var hash = location.hash.slice(1);
            router.trigger(hash);
        });
        window.addEventListener('load', function () {
            var hash = location.hash.slice(1) || 'default';
            router.trigger(hash);
        })
    };
    /**
     *将cache内的key 做正则处理,并返回
     * 第一个正则 匹配诸如/,.+-?$#{}[]] 关键字  并在关键字前面加转译字符\
     * 第二个正则 匹配() 标示()内部内容可有可无
     * 第三个正则 匹配: 在/后面可以由接受任意字符,直到遇到下一个/
     * 第四个正则 匹配* 在*后面可以由接受任意字符
     */
    this.initRegexps = function (route) {
        route = route.replace(/[/,.+\-?$#{}\[\]]/g, '\\    function Router() {
    this.cache = {};
    //将url/callback 以key/value形式储存在cache内
    this.on = function (key, value) {
        var cache = this.cache;
        cache[key] = value;
    };
    //匹配hash对应的回调函数,并触发
    this.trigger = function (hash) {
        var cache = this.cache;
        for (var r in cache) {
            var reg = this.initRegexps(r);
            if (reg.test(hash)) {
                var callback = cache[r] || function () {
                    };
                var params = this.getParams(reg, hash);
                callback.apply(this, params);
            }
        }

    };
    //初始化 添加监听浏览器hashchange 以及dom loaded函数
    this.init = function () {
        window.addEventListener('hashchange', function () {
            var hash = location.hash.slice(1);
            router.trigger(hash);
        });
        window.addEventListener('load', function () {
            var hash = location.hash.slice(1) || 'default';
            router.trigger(hash);
        })
    };
    /**
     *将cache内的key 做正则处理,并返回
     * 第一个正则 匹配诸如/,.+-?$#{}[]] 关键字  并在关键字前面加转译字符\
     * 第二个正则 匹配() 标示()内部内容可有可无
     * 第三个正则 匹配: 在/后面可以由接受任意字符,直到遇到下一个/
     * 第四个正则 匹配* 在*后面可以由接受任意字符
     */
    this.initRegexps = function (route) {
        route = route.replace(/[/,.+\-?$#{}\[\]]/g, '\\    function Router() {
    this.cache = {};
    //将url/callback 以key/value形式储存在cache内
    this.on = function (key, value) {
        var cache = this.cache;
        cache[key] = value;
    };
    //匹配hash对应的回调函数,并触发
    this.trigger = function (hash) {
        var cache = this.cache;
        for (var r in cache) {
            var reg = this.initRegexps(r);
            if (reg.test(hash)) {
                var callback = cache[r] || function () {
                    };
                var params = this.getParams(reg, hash);
                callback.apply(this, params);
            }
        }

    };
    //初始化 添加监听浏览器hashchange 以及dom loaded函数
    this.init = function () {
        window.addEventListener('hashchange', function () {
            var hash = location.hash.slice(1);
            router.trigger(hash);
        });
        window.addEventListener('load', function () {
            var hash = location.hash.slice(1) || 'default';
            router.trigger(hash);
        })
    };
    /**
     *将cache内的key 做正则处理,并返回
     * 第一个正则 匹配诸如/,.+-?$#{}[]] 关键字  并在关键字前面加转译字符\
     * 第二个正则 匹配() 标示()内部内容可有可无
     * 第三个正则 匹配: 在/后面可以由接受任意字符,直到遇到下一个/
     * 第四个正则 匹配* 在*后面可以由接受任意字符
     */
    this.initRegexps = function (route) {
        route = route.replace(/[/,.+\-?$#{}\[\]]/g, '\\    function Router() {
    this.cache = {};
    //将url/callback 以key/value形式储存在cache内
    this.on = function (key, value) {
        var cache = this.cache;
        cache[key] = value;
    };
    //匹配hash对应的回调函数,并触发
    this.trigger = function (hash) {
        var cache = this.cache;
        for (var r in cache) {
            var reg = this.initRegexps(r);
            if (reg.test(hash)) {
                var callback = cache[r] || function () {
                    };
                var params = this.getParams(reg, hash);
                callback.apply(this, params);
            }
        }

    };
    //初始化 添加监听浏览器hashchange 以及dom loaded函数
    this.init = function () {
        window.addEventListener('hashchange', function () {
            var hash = location.hash.slice(1);
            router.trigger(hash);
        });
        window.addEventListener('load', function () {
            var hash = location.hash.slice(1) || 'default';
            router.trigger(hash);
        })
    };
    /**
     *将cache内的key 做正则处理,并返回
     * 第一个正则 匹配诸如/,.+-?$#{}[]] 关键字  并在关键字前面加转译字符\
     * 第二个正则 匹配() 标示()内部内容可有可无
     * 第三个正则 匹配: 在/后面可以由接受任意字符,直到遇到下一个/
     * 第四个正则 匹配* 在*后面可以由接受任意字符
     */
    this.initRegexps = function (route) {
        route = route.replace(/[/,.+\-?$#{}\[\]]/g, '\\    function Router() {
    this.cache = {};
    //将url/callback 以key/value形式储存在cache内
    this.on = function (key, value) {
        var cache = this.cache;
        cache[key] = value;
    };
    //匹配hash对应的回调函数,并触发
    this.trigger = function (hash) {
        var cache = this.cache;
        for (var r in cache) {
            var reg = this.initRegexps(r);
            if (reg.test(hash)) {
                var callback = cache[r] || function () {
                    };
                var params = this.getParams(reg, hash);
                callback.apply(this, params);
            }
        }

    };
    //初始化 添加监听浏览器hashchange 以及dom loaded函数
    this.init = function () {
        window.addEventListener('hashchange', function () {
            var hash = location.hash.slice(1);
            router.trigger(hash);
        });
        window.addEventListener('load', function () {
            var hash = location.hash.slice(1) || 'default';
            router.trigger(hash);
        })
    };
    /**
     *将cache内的key 做正则处理,并返回
     * 第一个正则 匹配诸如/,.+-?$#{}[]] 关键字  并在关键字前面加转译字符\
     * 第二个正则 匹配() 标示()内部内容可有可无
     * 第三个正则 匹配: 在/后面可以由接受任意字符,直到遇到下一个/
     * 第四个正则 匹配* 在*后面可以由接受任意字符
     */
    this.initRegexps = function (route) {
        route = route.replace(/[/,.+\-?$#{}\[\]]/g, '\\    function Router() {
    this.cache = {};
    //将url/callback 以key/value形式储存在cache内
    this.on = function (key, value) {
        var cache = this.cache;
        cache[key] = value;
    };
    //匹配hash对应的回调函数,并触发
    this.trigger = function (hash) {
        var cache = this.cache;
        for (var r in cache) {
            var reg = this.initRegexps(r);
            if (reg.test(hash)) {
                var callback = cache[r] || function () {
                    };
                var params = this.getParams(reg, hash);
                callback.apply(this, params);
            }
        }

    };
    //初始化 添加监听浏览器hashchange 以及dom loaded函数
    this.init = function () {
        window.addEventListener('hashchange', function () {
            var hash = location.hash.slice(1);
            router.trigger(hash);
        });
        window.addEventListener('load', function () {
            var hash = location.hash.slice(1) || 'default';
            router.trigger(hash);
        })
    };
    /**
     *将cache内的key 做正则处理,并返回
     * 第一个正则 匹配诸如/,.+-?$#{}[]] 关键字  并在关键字前面加转译字符\
     * 第二个正则 匹配() 标示()内部内容可有可无
     * 第三个正则 匹配: 在/后面可以由接受任意字符,直到遇到下一个/
     * 第四个正则 匹配* 在*后面可以由接受任意字符
     */
    this.initRegexps = function (route) {
        route = route.replace(/[/,.+\-?$#{}\[\]]/g, '\\    function Router() {
    this.cache = {};
    //将url/callback 以key/value形式储存在cache内
    this.on = function (key, value) {
        var cache = this.cache;
        cache[key] = value;
    };
    //匹配hash对应的回调函数,并触发
    this.trigger = function (hash) {
        var cache = this.cache;
        for (var r in cache) {
            var reg = this.initRegexps(r);
            if (reg.test(hash)) {
                var callback = cache[r] || function () {
                    };
                var params = this.getParams(reg, hash);
                callback.apply(this, params);
            }
        }

    };
    //初始化 添加监听浏览器hashchange 以及dom loaded函数
    this.init = function () {
        window.addEventListener('hashchange', function () {
            var hash = location.hash.slice(1);
            router.trigger(hash);
        });
        window.addEventListener('load', function () {
            var hash = location.hash.slice(1) || 'default';
            router.trigger(hash);
        })
    };
    /**
     *将cache内的key 做正则处理,并返回
     * 第一个正则 匹配诸如/,.+-?$#{}[]] 关键字  并在关键字前面加转译字符\
     * 第二个正则 匹配() 标示()内部内容可有可无
     * 第三个正则 匹配: 在/后面可以由接受任意字符,直到遇到下一个/
     * 第四个正则 匹配* 在*后面可以由接受任意字符
     */
    this.initRegexps = function (route) {
        route = route.replace(/[/,.+\-?$#{}\[\]]/g, '\\$&')
            .replace(/\((.*?)\)/g, '(?:$1)?')
            .replace(/(\/\w?:\w+)+/g, '\/([^/]+)')
            .replace(/\*\w*/g, '([^?]*?)');

        return new RegExp('^' + route + '$');
    };

    //将匹配的正则返回,为回调函数提供参数
    this.getParams = function (reg, hash) {
        return reg.exec(hash).slice(1);
    }
}
')
            .replace(/\((.*?)\)/g, '(?:$1)?')
            .replace(/(\/\w?:\w+)+/g, '\/([^/]+)')
            .replace(/\*\w*/g, '([^?]*?)');

        return new RegExp('^' + route + '')
            .replace(/\((.*?)\)/g, '(?:$1)?')
            .replace(/(\/\w?:\w+)+/g, '\/([^/]+)')
            .replace(/\*\w*/g, '([^?]*?)');

        return new RegExp('^' + route + '')
            .replace(/\((.*?)\)/g, '(?:$1)?')
            .replace(/(\/\w?:\w+)+/g, '\/([^/]+)')
            .replace(/\*\w*/g, '([^?]*?)');

        return new RegExp('^' + route + '')
            .replace(/\((.*?)\)/g, '(?:$1)?')
            .replace(/(\/\w?:\w+)+/g, '\/([^/]+)')
            .replace(/\*\w*/g, '([^?]*?)');

        return new RegExp('^' + route + 'Copy the code

); }; This.getparams = function (reg, hash) {return reg.exec(hash).slice(1); } } ‘) .replace(/\((.*?) \)/g, ‘(? : $1)? ‘) .replace(/(\/\w? :\w+)+/g, ‘\/([^/]+)’) .replace(/\*\w*/g, ‘([^?] *?) ‘); return new RegExp(‘^’ + route + ”) .replace(/\((.*?) \)/g, ‘(? : $1)? ‘) .replace(/(\/\w? :\w+)+/g, ‘\/([^/]+)’) .replace(/\*\w*/g, ‘([^?] *?) ‘); return new RegExp(‘^’ + route + ”) .replace(/\((.*?) \)/g, ‘(? : $1)? ‘) .replace(/(\/\w? :\w+)+/g, ‘\/([^/]+)’) .replace(/\*\w*/g, ‘([^?] *?) ‘); return new RegExp(‘^’ + route + ‘); }; This.getparams = function (reg, hash) {return reg.exec(hash).slice(1); } } ‘) .replace(/\((.*?) \)/g, ‘(? : $1)? ‘) .replace(/(\/\w? :\w+)+/g, ‘\/([^/]+)’) .replace(/\*\w*/g, ‘([^?] *?) ‘); return new RegExp(‘^’ + route + ”) .replace(/\((.*?) \)/g, ‘(? : $1)? ‘) .replace(/(\/\w? :\w+)+/g, ‘\/([^/]+)’) .replace(/\*\w*/g, ‘([^?] *?) ‘); return new RegExp(‘^’ + route + ‘); }; This.getparams = function (reg, hash) {return reg.exec(hash).slice(1); } } ‘) .replace(/\((.*?) \)/g, ‘(? : $1)? ‘) .replace(/(\/\w? :\w+)+/g, ‘\/([^/]+)’) .replace(/\*\w*/g, ‘([^?] *?) ‘); return new RegExp(‘^’ + route + ‘); }; This.getparams = function (reg, hash) {return reg.exec(hash).slice(1); }}

index.html


    .test {
        width: 200px;
        height: 200px;
        color:white;
    }



       
Hash = / aaaa aaaa/BCD matches: id
Hash = BBBB matching/BBBB (/ : name)
Matching hash = BBBB/DDD/BBBB (/ : name)
Hash = CCCC/s/d match CCCC / *
Copy the code

The code address

Click here to see the full code

rendering

The flow chart

inductive

Although the implementation of this article is relatively simple, the internal routing of many frameworks is also based on this mechanism, with some optimization of their own characteristics.

The last

If this article is not perfect, or the flow chart needs to be improved, please correct.