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

推荐订阅源

Jina AI
Jina AI
V
Visual Studio Blog
博客园 - 司徒正美
TaoSecurity Blog
TaoSecurity Blog
博客园 - 聂微东
IT之家
IT之家
博客园_首页
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Cyber Attacks, Cyber Crime and Cyber Security
博客园 - Franky
雷峰网
雷峰网
罗磊的独立博客
S
Schneier on Security
C
Cybersecurity and Infrastructure Security Agency CISA
The Cloudflare Blog
T
Tailwind CSS Blog
B
Blog RSS Feed
H
Help Net Security
T
The Blog of Author Tim Ferriss
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threatpost
C
CERT Recently Published Vulnerability Notes
博客园 - 三生石上(FineUI控件)
P
Palo Alto Networks Blog
I
Intezer
G
GRAHAM CLULEY
Engineering at Meta
Engineering at Meta
S
Securelist
J
Java Code Geeks
V
V2EX
Y
Y Combinator Blog
Simon Willison's Weblog
Simon Willison's Weblog
L
LINUX DO - 热门话题
云风的 BLOG
云风的 BLOG
Spread Privacy
Spread Privacy
MongoDB | Blog
MongoDB | Blog
P
Privacy International News Feed
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
B
Blog
Forbes - Security
Forbes - Security
Google Online Security Blog
Google Online Security Blog
Help Net Security
Help Net Security
S
SegmentFault 最新的问题
N
Netflix TechBlog - Medium
Webroot Blog
Webroot Blog
Microsoft Security Blog
Microsoft Security Blog
SecWiki News
SecWiki News
Scott Helme
Scott Helme
aimingoo的专栏
aimingoo的专栏
N
News and Events Feed by Topic

博客园 - 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;
}