https://github.com/fundon/pathing
語法:默認使用{}
作爲標籤分隔符。
/posts/{id} 默認使用 [^/]+ 正則表達式.
/posts/{id:\\d+} 使用 `\d+` 正則表達式.
用例:
var pathing = require('pathing');
var tokens = pathing('/{controller}/{action}/{id:\\d+}');
/*
tokens:
[
{ name: 'DIVIDE', value: '/', pos: 0 },
{ name: 'PLACEHOLDER', value: 'controller', pos: 1, regexp: '[^/]+' },
{ name: 'DIVIDE', value: '/', pos: 13 },
{ name: 'PLACEHOLDER', value: 'action', pos: 14, regexp: '[^/]+' }
{ name: 'DIVIDE', value: '/', pos: 22 },
{ name: 'PLACEHOLDER', value: 'id', pos: 23, regexp: '\\d+' } ]
]
*/
var tokens = pathing('{year}-{month}-{day}');
/*
tokens:
[
{ name: 'PLACEHOLDER', value: 'year', pos: 0, regexp: '[^/]+' },
{ name: 'DASH', value: '-', pos: 6 },
{ name: 'PLACEHOLDER', value: 'month', pos: 7, regexp: '[^/]+' },
{ name: 'DASH', value: '-', pos: 14 },
{ name: 'PLACEHOLDER', value: 'day', pos: 15, regexp: '[^/]+' }
]
*/
自定義分隔符:
var tokens = pathing('/posts/<id>', { open: '<', close: '>' });
/*
tokens:
[
{ name: 'DIVIDE', value: '/', pos: 0 },
{ name: 'IDENTIFIER', value: 'posts', pos: 1 },
{ name: 'DIVIDE', value: '/', pos: 6 },
{ name: 'PLACEHOLDER', value: 'id', pos: 7, regexp: '[^/]+' }
]
*/