Skip to content Skip to sidebar Skip to footer

Uri Regex: Replace Http://, Https://, Ftp:// With Empty String If Url Valid

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

Solution 1:

var b = url.substr(url.indexOf('://')+3);

Solution 2:

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.

Solution 3:

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

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

Solution 4:

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

Post a Comment for "Uri Regex: Replace Http://, Https://, Ftp:// With Empty String If Url Valid"