[英]Regex to exclude URLs that contain file paths
我試圖只匹配那些不包含的網址?
char,不以\\
char結尾,並且不以文件路徑結尾(.jpg,.aspx等 - 需要排除所有文件擴展名)
這是預期的結果:
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
我的正則表達式 - [^.\\?]*[^/]*^[^?]*[^/]$
適用於大多數情況,但對於此http://mywebsite.com/some-path/test.jpg
失敗http://mywebsite.com/some-path/test.jpg
(匹配,但不是)
以下模式似乎有效:
^(?!.*\?)(?!.*\/[^/]+\.[^/]+$).*[^/]$
這使用兩個負面的前瞻來滿足您的要求:
(?!.*\?) - 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/'));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.