首页 > 解决方案 > 通过正则表达式解析url结构

问题描述

我有一个URL模式:

product/[cat]/[page].[ext]

产品/类别/page.html

产品/page.html

但是我的正则表达式不能正常工作:

^product\/([\w\d\.\-_\s\'\"\(\)\[\]\؀-\ۿ](?!.*\.html))*\/([\w\d\.\-_\s\'\"\(\)\[\]\؀-\ۿ]+\.html+)*\/?$

我想通过一种regEx模式检测 url 及其参数

match在javascript中使用函数

编辑 :

我的路线模式:

product/cat?/page.html?

我想regEx用这种模式制作

?在此模式中意味着此部分是可选的

例如 :

makeRegEx('product/cat?/page.html?')

结果:

^product\/([\w\d\.\-_\s\'\"\(\)\[\]\؀-\ۿ](?!.*\.html))*\/([\w\d\.\-_\s\'\"\(\)\[\]\؀-\ۿ]+\.html+)*\/?$

当路线: product/computer/ram.html

正则表达式检测:

cat = computer

page = ram.html

标签: javascriptregexurl

解决方案


这个正则表达式能解决你的问题吗?

^product\/([a-zA-Z]+)\/*([a-zA-Z]+)\.([a-zA-Z]+)

在我的Regex101上尝试一些案例


推荐阅读