简体   繁体   English

正则表达式匹配连续的-。 或字符串中的.-(连字符和点)

[英]Regular Expression to match consecutive -. or .- (hypen and dot) in a string

I have a scenario where consecutive -. 我有一个连续的情况。 or .- should not be allowed in a URL. 或.-不应在URL中使用。

Eg. 例如。 https://www.test.-nic or https://www.test-.nic whereas https://www.test.nic--xn/ should be allowed https://www.test.-nichttps://www.test-.nic而应允许https://www.test.nic--xn/

Can you help me improve this 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

(?=.*?(?:\\.-|-\\.) is a negative lookahead that will fail the match if ._ or -. are there in the URL. (?=.*?(?:\\.-|-\\.)是一个否定的超前查询,如果URL中存在._-.则匹配失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM