我正在研究一个节点应用程序,我需要一个正则表达式来匹配url模式并从url中获取信息,并提出可能的解决方案。正则表达式以匹配nodejs中的url模式
This are the url patterns:
1) www.mysite.com/Paper/cat_CG10
2) www.mysite.com/White-Copy-Printer-Paper/cat_DP5027
3) www.mysite.com/pen/directory_pen?
4) www.mysite.com/Paper-Mate-Profile-Retractable-Ballpoint-Pens-Bold-Point-Black-Dozen/product_612884
5) www.mysite.com/22222/directory_22222?categoryId=12328
These is what is want from the above url:
1) name= "cat" value="CG10"
2) name= "cat" value="DP5027"
3) name= "directory" value ="pen"
4) name="product" value ="612884"
5) name="directory" value="22222" params = {categoryId : 12328}
I want a regex which can match the url pattern and get the values like name, value and params out of the urls.
我想你给的例子。 var match = req.url.match(/^[^\/]+\/[^\/]+\/([^_]+)_([^\/_?]+).*$/) ; \t console.log(match);我在控制台中显示null,可能的原因是什么? – Manu
我应该看到不匹配的网址以及给你一个合适的答案 – Matyas
这是我得到的网址“/ HP-ENVY-TouchSmart-m7-j010dx-173-Touch-Screen-Refurbished-Laptop/product_8000”来自req.url中的nodejs请求对象。 – Manu