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 is www.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
 |  Show 20 more ments

3 Answers 3

Reset to default 4

Even 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\-]*)?(\?.*)?

  1. (https?:\/\/)? - start with http:// or https:// or not
  2. [\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.
  3. (\/[\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.
  4. (#[\w\-]*)? Followed maybe by an element id
  5. (\?.*)? 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