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)
答案 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/'));