简体   繁体   中英

Javascript pretty url regular expresison

I need to create regular expression for specific url router, for detail item. Here is written unit test, for expression.

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

For explonation, I need this:

http://localhost:8080/ -> home

http://localhost:8080/specific-detail-item -> detail

My current using expression is: /\\/[^/]+(.+)/

For detail expression accept, but for home is too. It shouldn't.

How can I solve it?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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