【发布时间】:2018-01-08 17:38:24
【问题描述】:
我正在添加一些简单的路由而不使用任何特定的框架。
当前正则表达式 -->
/^(\w)\/?(\w)\/?(\w)\/?(\d+)$/g
可能的 url 模式是
settings // should be matched to --> group1 = settings, group2 = null, group3 = null, group4 = null
settings/ // should be matched to --> group1 = settings, group2 = null, group3 = null, group4 = null
settings/205 // should be matched to --> group1 = settings, group2 = null, group3 = null, group4 = 205
settings/notifications // should be matched to --> group1 = settings, group2 = notifications, group3 = null, group4 = null
settings/notifications/ // should be matched to --> group1 = settings, group2 = notifications, group3 = null, group4 = null
settings/notifications/50 // should be matched to --> group1 = settings, group2 = notifications, group3 = null, group4 = 50
settings/notifications/pingers // should be matched to --> group1 = settings, group2 = notifications, group3 = pingers, group4 = null
settings/notifications/pingers/ // should be matched to --> group1 = settings, group2 = notifications, group3 = pingers, group4 = null
settings/notifications/pingers/101 // should be matched to --> group1 = settings, group2 = notifications, group3 = pingers, group4 = 101
非常感谢您的帮助!
【问题讨论】:
-
您已经尝试过的正则表达式在哪里,究竟是什么不起作用?
-
作为大多数“请给出代码”问题,这太宽泛了。请接tour,尤其是How to Ask。
-
在
/和?上拆分会不会容易很多? -
不,b/c 我需要填充所有这 4 个组,如果我拆分它,我需要循环遍历数组,这很昂贵。
标签: javascript regex