How to match only extensionless URLs?

2020-04-11 11:32发布

I want a regex expression which only match extensionless url. In otherwords if an extension is present in url then completely ignore it.

/(.+)/(.+) this will match an URL both with and without extension.

www.site.com/sports/cricket should match

www.site.com/sports/cricket.aspx shouldn't match

Thanks in advance

3条回答
Juvenile、少年°
2楼-- · 2020-04-11 11:57
^/(.+)/([^/.]*)$

will match a URL (without a domain) that contains no dot after the last slash.

EDIT: Adapted it better to the original regex.

查看更多
\"骚年 ilove
3楼-- · 2020-04-11 12:00

The following will only match strings which have at least two / (per your example regex), and don't have a . anywhere after the last /:

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

I'd recommend http://www.regular-expressions.info/ if you want to learn more about regexs.

查看更多
男人必须洒脱
4楼-- · 2020-04-11 12:16
.+/[^./]*$

This will match strings with no . after last /

查看更多
登录 后发表回答