admin管理员组文章数量:1334802
I'm looking for a regex that accept urls like these:
www.example
This is what I have so far, but it regex doesn't match URLs without http://
or https://
, or ftp://
:
regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
How can I make the protocol optional?
I'm looking for a regex that accept urls like these:
http://www.example.com
www.example.com
This is what I have so far, but it regex doesn't match URLs without http://
or https://
, or ftp://
:
regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
How can I make the protocol optional?
Share Improve this question edited Apr 22, 2015 at 12:40 anon asked Nov 25, 2010 at 9:34 GuestGuest 2172 gold badges3 silver badges7 bronze badges 5 |6 Answers
Reset to default 16Make the (ftp|http|https):\/\/
part optional:
((ftp|http|https):\/\/)?
Try this this will validate url with (http,ftp,https) or without(http,ftp,https)..
/^(?:(ftp|http|https):\/\/)?(?:[\w-]+\.)+[a-z]{3,6}$/;
Try this this will validate url with or without(http,ftp,https) in upper and lower cases and also allows you to for numerics
/^(?:(ftp|http|https)?:\/\/)?(?:[\w-]+\.)+([a-z]|[A-Z]|[0-9]){2,6}$/gi;
You can use the next reg:
^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$
Here is an example: https://www.regextester.com/93652
Kindly see https://codegolf.stackexchange.com/a/480/6593
Quoting from the above link:
value = 'www.google.com';
if(/(^|\s)((https?:\/\/)?[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?)/gi.test(value)) {
return true;
} else {
return false;
}
regex accept url without http or https
[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@%_\+.~#?&//=]*)
In HTML5
<input pattern="[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@%_\+.~#?&//=]*)" type="text>
本文标签: javascriptRegex for urls without httpHTTPSftpStack Overflow
版权声明:本文标题:javascript - Regex for urls without http, https, ftp - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1737975240a2043599.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
example.pl
: A website in Poland or a Perl script? – Boldewyn Commented Nov 25, 2010 at 9:44