javascript - Regular Expression to match consecutive -. or .- (hypen and dot) in a string -
i have scenario consecutive -. or .- should not allowed in url.
eg. https://www.test.-nic or https://www.test-.nic whereas https://www.test.nic--xn/ should allowed
can me improve regular expression?
/^(http|https|ftp):\/\/[a-z0-9]+(.[a-z0-9-]+)*.[a-z0-9]{2,5}(:[0-9]{1,5})?(\/.-*)?$/i
you can use negative lookahead:
/^(https?|ftp):\/\/(?=.*?(?:\.-|-\.)[a-z0-9]+(\.[a-z0-9-]+)*\.[a-z0-9]{2,5}(:[0-9]{1,5})?$/i (?=.*?(?:\.-|-\.) negative lookahead fail match if ._ or -. there in url.
Comments
Post a Comment