Regex to exclude URLs that contain file paths

时间:2018-10-02 09:15:14

标签: javascript regex

I'm trying to match only URLs that does'n contain ? char, that doesn't end with \ char and that doesn't end with a file path (.jpg, .aspx etc - need to exclude all file extensions)

This is expected result:

  1. Doesn't match - http://mywebsite.com/some-path/test.jpg
  2. Doesn't match - http://mywebsite.com/some-path/test.jpg/
  3. Match - http://mywebsite.com/some-path/test
  4. Doesn't match - http://mywebsite.com/some-path/test?v=ASAS77162UTNBYV77

My regex - [^.\?]*[^/]*^[^?]*[^/]$, works well for most cases, but fail for this http://mywebsite.com/some-path/test.jpg (matches, but it doesn't)

1 个答案:

答案 0 :(得分:2)

以下模式似乎有效:

^(?!.*\?)(?!.*\/[^/]+\.[^/]+$).*[^/]$

这会使用两个否定的前行来满足您的要求:

(?!.*\?)                - no ? appears anywhere in the URL
(?!.*\/[^\/]+\.[^\/]+$) - no extension appears

通过在URL的每个末尾匹配该字符,从字面上给出了URL不以路径分隔符结尾的要求。

console.log(/^(?!.*\?)(?!.*\/[^/]+\.[^/]+$).*[^/]$/.test('http://mywebsite.com/some-path/test'));
console.log(/^(?!.*\?)(?!.*\/[^/]+\.[^/]+$).*[^/]$/.test('http://mywebsite.com/some-path/test.jpg'));
console.log(/^(?!.*\?)(?!.*\/[^/]+\.[^/]+$).*[^/]$/.test('http://mywebsite.com/some-path/test?v=ASAS77162UTNBYV77'));
console.log(/^(?!.*\?)(?!.*\/[^/]+\.[^/]+$).*[^/]$/.test('http://mywebsite.com/some-path/test.jpg/'));