最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - URI Regex: Replace http:, https:, ftp: with empty string if URL valid - Stack Overflow

programmeradmin8浏览0评论

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?

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?

Share Improve this question asked Jan 10, 2012 at 11:42 jQuerybeastjQuerybeast 14.5k39 gold badges119 silver badges198 bronze badges 8
  • because protomatch.test(url) returns true or false, not a string – Hoff 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. – Tomalak 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 – jQuerybeast 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). – Tomalak 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?! – Alnitak Commented Jan 10, 2012 at 13:22
 |  Show 3 more comments

4 Answers 4

Reset to default 26
var b = url.substr(url.indexOf('://')+3);

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.

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

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

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论