【发布时间】:2016-10-04 01:32:38
【问题描述】:
有什么有效/好的方法来匹配 URL 中的 slug 吗?
例如:
匹配:
- http://somepage.com/s/XXXXX/this-is-slug-i-want
- http://somepage.com/s/XXXXX/this-is-slug-i-want/
- http://somepage.com/s/XXXXX/this-is-slug-i-want?foo=bar
- http://somepage.com/s/XXXXX/this-is-slug-i-want#foo
不匹配:
- http://somepage.com/s/XXXXX/somePage.html 和类似的(.php 等)
【问题讨论】:
-
在 Stack Overflow 上提问之前,请至少尝试解决方案。我们不是代码编写服务。另外,正如How to Ask 中所说,请在询问之前进行研究,关于这个主题有很多问题。
-
我会选择
.*this-is-slug-i-want.* -
这很不清楚。您希望字符串与正则表达式匹配吗?你想比较最后一个蛞蝓吗?你想完成什么?
-
@sin 这是不正确的,该正则表达式将匹配
http://this-is-slug-i-want.com、http://somepage.coms/XXXXX/somePage.html?this-is-slug-i-want以及 OP 绝对不想要的大量其他内容
标签: javascript regex