Reputation: 6517
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:
http://mywebsite.com/some-path/test.jpg
http://mywebsite.com/some-path/test.jpg/
http://mywebsite.com/some-path/test
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
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