javascript split() 正则表达式

路由匹配

 http.createServer(function(req, res) {
        var items = req.url.split('/');
        if (items.length < 3 || items[1] == 'img') {
            execRequest(req, res)
        } else {
            var path = req.url.split(/[?#]/)[0],
                projectConf;
            config.some(function(item) {
                if (path.indexOf(item.alias) == 0) {
                    projectConf = item;
                    path = '/' + item.name + path.substring(item.alias.length);
                    return true;
                }
            });
        //此处再写其他逻辑 
    }
});

代码分析 :

  1. req.url.split(/[?#]/)[0]
    1. 正则 /[?#]/
    2. [] 一个字符集合。 匹配方括号中的任意字符。
    3. 字符转义
  2. router匹配开头,避免层级目录中有符合匹配项 替换导致出错
原文地址:https://www.cnblogs.com/yingwo/p/5784201.html