admin管理员组文章数量:1435859
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
4 Answers
Reset to default 26var 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.
本文标签:
版权声明:本文标题:javascript - URI Regex: Replace http:, https:, ftp: with empty string if URL valid - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1737500575a1993021.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论