Regex to validate both web and LDAP url
Regex to validate both web and LDAP url
我正在创建一个Regex ^(http://|https://|ldap://|ldaps://){1}['S]*$
来验证这些类型的URL
- http://stackoverflow.com
- http://www.stackoverflow.com
- https://stackoverflow.com?q=regular+expr=org.mozilla:en-US:official&client=firefox-a
- ldap://192.442.242.244:389
它不应该验证
- 错误的url
- http://wwsw.stackoverflow.com
- ldap://192.442.242.244:389asd
- ldap://192.442.242asd.244:389
我将把它用于javascript
m{ (?:ldap|http)s?:// # Scheme
(?:(?!.*'d[/?:]) [a-z0-9'-._~%]+ | # IPv4 host (forbid ending with digit)
# IP (yep, I've copied this regexp)
(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])'.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])
'[[a-z0-9'-._~%!$&'()*+,;=:]+'] ) # or IPv6 host
(?::'d+)? # Port
(?:
[/?] # Slash or question mark
[-A-Z0-9+&@#/%?=~_|$!:,.;]* # URL itself
)? # Technically, trailing / is required in URLs, but many browser implementations ignore it when accepting those
}ix
这个正则表达式应该与/ix
一起使用。由于JS不包含/x
修饰符,我将包含没有它的版本
/(?:ldap|http)s?:'/'/(?:(?!.*'d['/?:])[a-z0-9'-._~%]+|(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])'.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])'[[a-z0-9'-._~%!$&'()*+,;=:]+'])(?::'d+)?(?:['/?]['-A-Z0-9+&@#'/%?=~_|$!:,.;]*)?/i
请注意,HTTP URL中允许使用大量字符,而无需使用%
标记进行转义。您可能不希望URL不通过,因为它包含URL中允许的字符*
:)。
对于可以使用的IP^(http://|https://|ldap://|ldaps://)((''d{1,3}''.){3}''d{1,3}(:''d{1,5})?)$
但对于其他URL。。。您必须检查站点是否响应。因为它可以是一个网站,比如wwsw.com
,你的正则表达式应该如何知道它是不是一个真正的网站?可以有一个真正的网站的子域wwsw
编辑:
GlitchMr是对的。对于IPv4,请使用他的正则表达式。
相关文章:
- Selenium WebDriver and JavaScript change
- PHP and Javascript functions
- Javascript Return and if/else
- SetTimeout and clearTimeout in Javascript
- lightbox in html 5 and javascript
- TimelineJS and AngularJS
- timeago.js with datatable and PHP
- javascript button ajax and php
- Ajax and Json with Rails
- Combine onload, onresize and onclick
- php布尔值's小写AND大写和数字布尔值'可以接受
- setTimeout and V8
- Jquery post and onclick
- Moment js and IOS
- Rails 3.2 and Paloma gem
- Backbone, TypeScript, and super
- iframe and JavaScript on different pages
- JSON and jQuery Search
- Flash and External Javascript
- Regex to validate both web and LDAP url