Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我希望我的正则表达式匹配所有不以结尾的有效URL
.gif .jpg .jpeg .pdf .doc
我试过了
http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=;]*)?((?!jpg)|(?!gif)|(?!doc))
您需要为此使用lookbehind,尝试
http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=;]*)?(?<!jpg)(?<!gif)(?<!doc)$
您还需要末尾的锚点$,它与字符串的末尾匹配,这对于清楚地定义后视应该向后看的点很重要。
$
在 Regexr 上查看