admin管理员组文章数量:1401431
I'm working on create a regular expression in javascript to validate website urls. I searched a bit in the stackoverflow munity and i did not find something to be pleted helpful.
My regex until now: /(https?:\/\/)?(www\.)?[a-zA-Z0-9]+\.[a-zA-Z]{2,}/g
But it seems to fail and pass the validation for the url with two w like ww.test
Should pass the test of regex:
www.test
www.test.co.uk
www.t
test
test.fr
test.co.uk
Should not pass the test of regex:
w.test
ww.test
www.test
test
ww.test.
.test
.test
.test.co.ul
.test.
Any suggestions or thoughts?
I'm working on create a regular expression in javascript to validate website urls. I searched a bit in the stackoverflow munity and i did not find something to be pleted helpful.
My regex until now: /(https?:\/\/)?(www\.)?[a-zA-Z0-9]+\.[a-zA-Z]{2,}/g
But it seems to fail and pass the validation for the url with two w like ww.test.
Should pass the test of regex:
http://www.test.
https://www.test.
www.test.
www.test.co.uk
www.t.
test.
test.fr
test.co.uk
Should not pass the test of regex:
w.test.
ww.test.
www.test
test
ww.test.
.test
.test.
.test.co.ul
.test.
Any suggestions or thoughts?
Share Improve this question asked Aug 30, 2016 at 6:05 PanagiotisPanagiotis 2611 gold badge3 silver badges18 bronze badges 25-
1
So
Mr.Bean
is also a valid URL? – anubhava Commented Aug 30, 2016 at 6:06 -
3
"ww.test."
is a valid website address - why wouldn't it be? So iswww.test-site.
, but your regex doesn't allow for hyphens. – nnnnnn Commented Aug 30, 2016 at 6:06 -
1
@Panagiotis Why not? What if some sites were accessed in this fashion
someurl.user.
You're confusing URL with a domain name – Aaron Commented Aug 30, 2016 at 6:10 - 2 @Panagiotis What you're trying to do is impossible with regex. A URL could be valid but could not exist, so you need to resolve the URL on a valid URL. – Aaron Commented Aug 30, 2016 at 6:16
-
2
@Panagiotis Not sure if this is an option (Don't see why not), but you can set
<input type="url">
Have a look a this question if it's a possibility stackoverflow./questions/13820477/… – Aaron Commented Aug 30, 2016 at 6:30
3 Answers
Reset to default 4Even if this answer is a bit too much for this Problem, it illustrates the problem: Even if it might be possible to create a regexp to check the url, it is much simpler and more robust to parse the URL and "create a real Object", on/with which the overall test can be deposed to a number of smaller tests.
So probably the builtin URL
constructor of modern browsers may help you here (LINK 1, LINK 2).
One approach to test you url might look like this:
function testURL (urlstring) {
var errors = [];
try {
var url = new URL(urlstring);
if (!/https/.test(url.protocol)) {
errors.push('wrong protocol');
}
//more tests here
} catch(err) {
//something went really wrong
//log the error here
} finally {
return errors;
}
}
if (testURL('mr.bean').length == 0) { runSomething(); }
Here's a non official, but works for most things one with an explanation. This should be good enough for most situations.
(https?:\/\/)?[\w\-~]+(\.[\w\-~]+)+(\/[\w\-~]*)*(#[\w\-]*)?(\?.*)?
(https?:\/\/)?
- start withhttp://
orhttps://
or not[\w\-~]+(\.[\w\-~]+)+
follow it with the domain name[\w\-~]
and at least one extension(\.[\w\-~])+
[\w\-~]
==[a-zA-Z0-9_\-~]
- Multiple extensions would mean
test.go.place.
(\/[\w\-~]*)*
then as many sub directories as wished- In order to easily make
test./
pass, the slash does not enforce following characters. This can be abused like so:test./la////la
.
- In order to easily make
(#[\w\-]*)?
Followed maybe by an element id(\?.*)?
Followed maybe by url params, which (for the sake of simplicity) can be pretty much whatever
There are plenty of edge cases where this will break, or where it should but it doesn't. But, for most cases where people aren't doing anything wacky, this should work.
/((http|https)\:\/\/)?[a-zA-Z0-9\.\/\?\:@\-_=#]+\.([a-zA-Z0-9\&\.\/\?\:@\-_=#])*/g
本文标签: Javascript Website url validation with regexStack Overflow
版权声明:本文标题:Javascript: Website url validation with regex - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744269962a2598132.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论