Vnuuk
Vnuuk

Reputation: 6517

Regex to exclude URLs that contain file paths

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)

Upvotes: 3

Views: 687

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520908

The following pattern seems to be working:

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

This uses two negative lookaheads to meet your requirements:

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

The requirement for the URL not ending in a path separator is given literally, by matching that character at the every end of the 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/'));

Upvotes: 2

Related Questions