12

I have a simple URL validator. The url validator works as probably every other validator.

Now I want, if the URL passed, take the https://, http:// and remove it for var b.

So what I've done is I made a another Regex which captures https://, http://, ftp:// etc and say if the url passed the long test, get the second test and replace it with empty string.

Here is what I came up with:

$("button").on('click', function () {
   var url = $('#in').val();

   var match = /^([a-z][a-z0-9\*\-\.]*):\/\/(?:(?:(?:[\w\.\-\+!$&'\(\)*\+,;=]|%[0-9a-f]{2})+:)*(?:[\w\.\-\+%!$&'\(\)*\+,;=]|%[0-9a-f]{2})+@)?(?:(?:[a-z0-9\-\.]|%[0-9a-f]{2})+|(?:\[(?:[0-9a-f]{0,4}:)*(?:[0-9a-f]{0,4})\]))(?::[0-9]+)?(?:[\/|\?](?:[\w#!:\.\?\+=&@!$'~*,;\/\(\)\[\]\-]|%[0-9a-f]{2})*)?$/;
   var protomatch = /^(https?|ftp):\/\/(.*)/;


   if (match.test(url)) { // IF VALID
      console.log(url + ' is valid');

      // if valid replace http, https, ftp etc with empty
      var b = url.replace(protomatch.test(url), '');
      console.log(b)

   } else { // IF INVALID
      console.log('not valid')
   }

});

Why this doesn't work?

8
  • because protomatch.test(url) returns true or false, not a string Commented Jan 10, 2012 at 11:54
  • 2
    The regex you're using there is well beyond the bounds of sanity. You must break it up, otherwise it is an unmanageable, unintelligible, unmaintainable heap of characters and in this state it won't do you any good. Try a staged approach: Stage one: Basic structural test, catching those basic structures into groups. Stage 2: More basic tests on the groups to determine their contents: Stage 3: Individual validity tests and value extraction from those. Yes, it is a little more code, but it won't make your head hurt and it will have less bugs. Commented Jan 10, 2012 at 11:56
  • @tomalak I choosed it from here: mathiasbynens.be/demo/url-regex. Im struggling to create my own entire Regex and I find this to fit my needs Commented Jan 10, 2012 at 12:48
  • 1
    @jQuerybeast Well, if you can't understand it, you should not use it. And frankly, this one is not okay (I'm good with regex, I can tell). Commented Jan 10, 2012 at 13:06
  • So you went to that website, and didn't pick the one that passed all of the tests?! Commented Jan 10, 2012 at 13:22

4 Answers 4

26
var b = url.substr(url.indexOf('://')+3);
Sign up to request clarification or add additional context in comments.

2 Comments

this will remove any URL scheme identifier, not just https, http and ftp.
what happens if the url itself not contains http or https at some cases.
23

protomatch.test() returns a boolean, not a string.

I think you just want:

var protomatch = /^(https?|ftp):\/\//; // NB: not '.*'
...
var b = url.replace(protomatch, '');

FWIW, your match regexp is completely impenetrable, and almost certainly wrong. It probably doesn't permit internationalised domains, but it's so hard to read that I can't tell for sure.

1 Comment

Great. I should test protomotach.test() on console. Thank you.
2

If you want a more generic approach, like Yuri's, but which will work for more cases:

var b = url.replace(/^.*:\/\//i, '');

Comments

0

There may be an empty protocol, like: //example.com, so relying on '://' may not cover all cases.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.