惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
The Blog of Author Tim Ferriss
Hugging Face - Blog
Hugging Face - Blog
F
Fortinet All Blogs
B
Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Microsoft Security Blog
Microsoft Security Blog
Blog — PlanetScale
Blog — PlanetScale
月光博客
月光博客
腾讯CDC
小众软件
小众软件
G
Google Developers Blog
V
Visual Studio Blog
罗磊的独立博客
GbyAI
GbyAI
V
V2EX
大猫的无限游戏
大猫的无限游戏
H
Help Net Security
L
LangChain Blog
Engineering at Meta
Engineering at Meta
量子位
The GitHub Blog
The GitHub Blog
博客园 - 司徒正美
WordPress大学
WordPress大学
B
Blog RSS Feed

博客园 - Q.Lee.lulu

aProxy: 带认证授权和权限控制的反向代理 Nginx做前端Proxy时TIME_WAIT过多的问题 Cubieboard通过aria2和minidlna来架设家庭媒体中心 与IT&码农有关的电影和记录片 golang与node.js的http模块性能对比测试(go1) 用OpenCv来做人脸识别 linux下SublimeText的中文输入法问题之解决方案 golang与node.js的http对比测试 作为Web开发人员,我为什么喜欢Google Chrome浏览器 sqlalchemy在web.py中的session使用 web.py大文件下载 Python和Node.js支持尾递归吗? 小文件、nginx、Redis、Moosefs 一道JavaScript面试题(setTimeout) 用Eclipse调试Node.js代码 Javascript中的类数组对象 抛弃Fastcgi,用uwsgi来部署你的Django程序吧 Node.js:用JavaScript写服务器端程序-介绍并写个MVC框架 FaWave(发微)-Chrome上的多微博全能插件
Javascript正则分组命名
Q.Lee.lulu · 2011-03-16 · via 博客园 - Q.Lee.lulu

Javascript的正则分组不支持命名,只好自己搞了一个。先把命名存入数组,然后匹配。

唉~~~有更好的解决方案么?

image

代码:

var route = '/{controller}/{action}/{id}',
    url = '/home/index/2';

groupRE(route, url); // ==> {controller:'home', action:'index', id:'2'}

/*
* @re: string, e.g.: '/{controller}/{action}/{id}'
* @s: string to match, e.g.: 'home/index/2'
* @return: dict, e.g.: {controller:'home', action:'index', id:'2'}
*/
function groupRE(re, s){
    var names = [], result = {}, cursor = 0;
    re = re.replace(/\{([^}]+)\}/g, function(m, g1){
        names.push(g1);
        return '(.+)';
    });
    re = new RegExp(re);
    var tmp = re.exec(s);
	if(tmp){
		for(var i=1; i<tmp.length; i++){
			if(names[i-1]){
				result[ names[i-1] ] = tmp[i];
			}
		}
	}
    return result;
}