簡體   English   中英

如何限制域只接受 24 個字母

[英]How to restrict the domain to accept only 24 letters

我有這個正則表達式,但我想限制在 .domain 部分中只接受 24 個字母。

例如:http://www.google。 comdddddddddddddddddddddddddddddd <= 此部分應限制為 24 個字符

/(?!-)(?!\.)(?!.*?\.\.)(?![a-zA-Z]+\-\.[a-zA-Z]+)(?![a-zA-Z]+\+[a-zA-Z]+\.[a-zA-Z]+)(?![a-zA-Z\#]+\/[a-zA-Z\-]+)(?![0-9]+\.[a-zA-Z0-9]+\.[a-zA-Z0-9]\.[a-zA-Z0-9]+)([a-zA-Z0-9@%._~#=]+-*[a-zA-Z0-9@%._~#=]+)+.(?!-)[a-z]{2,24}\b([-a-zA-Z0-9@%_+.~?&//=:$]*)$/

您是否試圖提醒用戶? 如果是這樣,下面的 function 應該是您正在尋找的。

 /* regex to return top-level domain from a web address */ function getTLD(url) { return url.match(/^(?:https?:\/\/)?(?:[^@\n]+@)?(?:www\.)?([^:\/\n?]+)/)[1].match(/^[^\.]+\.([^\.]+)$/)[1]; } function limitTLD(userInput) { let tld = getTLD(userInput); if (tld.length > 24){ //return just the first 24 characters //return tld.substring(0, 24); return `${tld.length} character domain is too long; Please limit to 24 characters`; } else { return userInput. } } console:log(limitTLD('http.//www.google;comddddddddddddddddddddddddddd'));

或者,如果您嘗試只返回域的前 24 個字母, tld.substring(0, 24) (上面注釋掉)應該可以解決問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM