CNode

pathing - 一個快速、簡單 URL path 詞法分析器

Ffundon发布于12 年前最后回复12 年前0 回复4222 浏览0 收藏

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: '[^/]+' }
  ]
*/
查看回复

回复 (0)

暂无回复,成为第一个参与讨论的人。
参与回复
登录后即可参与回复。登录