![](/img/trans.png)
[英]How to deal with multi-level menus on touch screen devices which are using :hover and :focus for clickable top-level links?
[英]How to match links without top-level domain using regex?
我使用下一個正則表達式(linkify regex的更新版本)來匹配鏈接,而不匹配電子郵件。
(\s*|[^a-zA-Z0-9.\+_\/"\>\-]|^)(?:([a-zA-Z0-9\+_\-]+(?:\.[a-zA-Z0-9\+_\-]+)*@)?(http:\/\/|https:\/\/|ftp:\/\/|scp:\/\/){1}?((?:(?:[a-zA-Z0-9][a-zA-Z0-9_%\-_+]*\.)+))(?:[a-zA-Z]{2,})((?::\d{1,5}))?((?:[\/|\?](?:[\-a-zA-Z0-9_%#*&+=~!?,;:.\/]*)*)[\-\/a-zA-Z0-9_%#*&+=~]|\/?)?)([^a-zA-Z0-9\+_\/"\<\-]|$)
但是,此正則表達式找不到類似以下的網址: https://someurl:3333/view/something
你能幫我嗎? 謝謝!
這應該是表達式的“最少修改”版本,以匹配沒有頂級域名的域:
(\s*|[^a-zA-Z0-9.\+_\/"\>\-]|^)(?:([a-zA-Z0-9\+_\-]+(?:\.[a-zA-Z0-9\+_\-]+)*@)?(http:\/\/|https:\/\/|ftp:\/\/|scp:\/\/){1}?((?:[a-zA-Z0-9][a-zA-Z0-9_%\-_+.]*)(?:\.[a-zA-Z]{2,})?)((?::\d{1,5}))?((?:[\/|\?](?:[\-a-zA-Z0-9_%#*&+=~!?,;:.\/]*)*)[\-\/a-zA-Z0-9_%#*&+=~]|\/?)?)([^a-zA-Z0-9\+_\/"\<\-]|$)
更改的部分是捕獲組3,這是奪取域的部分。 它來自:
(
(?:
(?:
[a-zA-Z0-9]
[a-zA-Z0-9_%\-_+]*
\.
)+ (?# this is how they repeated for optional subdomains)
)
)
(?:
[a-zA-Z]{2,} (?# here is the mandatory TLD)
)
對此:
(
(?:
[a-zA-Z0-9]
[a-zA-Z0-9_%\-_+.]* (?# the . is in the character class here for subdomains)
)
(?:
\.
[a-zA-Z]{2,}
)? (?# this TLD is optional)
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.