简体   繁体   English

Javascript漂亮的URL常规表达式

[英]Javascript pretty url regular expresison

I need to create regular expression for specific url router, for detail item. 我需要为特定的URL路由器创建正则表达式,以获取详细信息。 Here is written unit test, for expression. 这是书面的单元测试,用于表达。

https://regex101.com/r/LUo5k9/2/tests https://regex101.com/r/LUo5k9/2/tests

For explonation, I need this: 为了说明问题,我需要这样做:

http://localhost:8080/ -> home http:// localhost:8080 / ->主页

http://localhost:8080/specific-detail-item -> detail http:// localhost:8080 / specific-detail-item- >详细信息

My current using expression is: /\\/[^/]+(.+)/ 我当前使用的表达式是:/ /\\/[^/]+(.+)/

For detail expression accept, but for home is too. 对于细节表达,可以接受,但对于家庭也是。 It shouldn't. 不应该这样

How can I solve it? 我该如何解决?

您需要将/明确要求为捕获组中的第一个字符,否则[^/]+可以匹配的字符数少于您的预期:

\\/[^/]+(\\/.+)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM