I have the following strings
http://site.com
https://site.com
http://www.site.com
how do i get rid of the http:// or https:// in javascript or jquery
I have the following strings
http://site.com
https://site.com
http://www.site.com
how do i get rid of the http:// or https:// in javascript or jquery
var txt="https://site.com";
txt=/^http(s)?:\/\/(.+)$/i.exec(txt);
txt=txt[2];
for parsing links without http/https use this:
var txt="https://site.com";
txt=/^(http(s)?:\/\/)?(.+)$/i.exec(txt);
txt=txt[3];
var str = "https://site.com";
str = str.substr( str.indexOf(':') + 3 );
Instead of .substr()
, you could also use .slice()
or .substring()
. They'll all produce the same result in this situation.
str = str.slice( str.indexOf(':') + 3 );
str = str.substring( str.indexOf(':') + 3 );
EDIT: It appears as though the requirements of the question have changed in a comment under another answer.
If there possibly isn't a http://
in the string, then do this:
var str = "site.com";
var index = str.indexOf('://');
if( index > -1 )
str = str.substr( index + 3 );
Try with this:
var url = "https://site.com";
var urlNoProtocol = url.replace(/^https?\:\/\//i, "");