Get protocol, domain, and port from URL
first get the current address
var url = window.location.href
Then just parse that string
var arr = url.split("/");
your url is:
var result = arr[0] + "//" + arr[2]
Hope this helps
Java | API to get protocol://domain.port from URL
Create a new URL
object using your String
value and call getHost()
or any other method on it, like so:
URL url = new URL("https://test.domain.com/a/b/c.html?test=hello");
String protocol = url.getProtocol();
String host = url.getHost();
int port = url.getPort();
// if the port is not explicitly specified in the input, it will be -1.
if (port == -1) {
return String.format("%s://%s", protocol, host);
} else {
return String.format("%s://%s:%d", protocol, host, port);
}
Get protocol, hostname, and path from URL
Your regex won't capture https://www.google.com
.
Use capturing group and apply your regex with regex.exec()
. Then access the returned array to set your variable:
str="https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=picture%20of%20a%20potato";regex = new RegExp('(https?://.*?\)/');match = regex.exec(str)[1];console.log(match);
.NET - Get protocol, host, and port
The following (C#) code should do the trick
Uri uri = new Uri("http://www.mywebsite.com:80/pages/page1.aspx");
string requested = uri.Scheme + Uri.SchemeDelimiter + uri.Host + ":" + uri.Port;
Get protocol and domain (WITHOUT subdomain) from a URL
I am using tldextract
When I doing the domain parse.
In your case you only need combine the domain
+ suffix
import tldextract
tldextract.extract('mail.google.com')
Out[756]: ExtractResult(subdomain='mail', domain='google', suffix='com')
tldextract.extract('classes.usc.edu/xxx/yy/zz')
Out[757]: ExtractResult(subdomain='classes', domain='usc', suffix='edu')
tldextract.extract('google.co.uk')
Out[758]: ExtractResult(subdomain='', domain='google', suffix='co.uk')
Get protocol + host name from URL
You should be able to do it with urlparse
(docs: python2, python3):
from urllib.parse import urlparse
# from urlparse import urlparse # Python 2
parsed_uri = urlparse('http://stackoverflow.com/questions/1234567/blah-blah-blah-blah' )
result = '{uri.scheme}://{uri.netloc}/'.format(uri=parsed_uri)
print(result)
# gives
'http://stackoverflow.com/'
Getting port from URL string using Javascript
From what I get, you don't want to use location
as the URL to subtract the port from, just any string as an URL. Well, I came up with this, for such a case. This function takes any string (but you can pass it the location
URL anyway, and it works the same):
function getPort(url) {
url = url.match(/^(([a-z]+:)?(\/\/)?[^\/]+).*$/)[1] || url;
var parts = url.split(':'),
port = parseInt(parts[parts.length - 1], 10);
if(parts[0] === 'http' && (isNaN(port) || parts.length < 3)) {
return 80;
}
if(parts[0] === 'https' && (isNaN(port) || parts.length < 3)) {
return 443;
}
if(parts.length === 1 || isNaN(port)) return 80;
return port;
}
- It gets the base url from the string.
- It splits the base url into parts, by
':'
. - It tries to parse the digits-only part of the port (the last element of the
parts
array) into an integer. - If the URL starts with
'http'
AND the port is not a number or the length of the URL parts array is less than 3 (which means no port was implied in the URL string), it returns the default HTTP port. - Same thing goes for
'https'
. - If the length was
1
, it means no protocol nor port was provided. In that case or in the case the port is not a number (and again, no protocol was provided), return the defaultHTTP
port. - If it passes through all these tests, then it just returns the port it tried to parse into an integer at the beginning of the function.
Get the website protocol when I have the address
You can achieve this with the following command:
openssl s_client -showcerts -connect google.com:443
This assumes that the server you are testing is setup to use the standard https port 443
.
Return only protocol and domain from full URI string
sometodo("http://127.0.0.1:8000/hello/some/word1212/");
sometodo("http://127.0.0.1:8000/hello/some/valorant_operator/");
function sometodo(str) {
var output = str.match(/^(?:[^\/]*\/){3}/)[0];
console.log(output);
return output;
}
Related Topics
Remove All White Spaces from Text
Turning Off Eslint Rule for a Specific Line
Using Address Instead of Longitude and Latitude with Google Maps API
How to Share Code Between Node.Js and the Browser
Chrome, JavaScript, Window.Open in New Tab
How to Add Extra Info to Copied Web Text
JSON.Stringify() Array Bizarreness with Prototype.Js
Jquery.Inarray(), How to Use It Right
How to Create Dynamic Variable Names Inside a Loop
What's the Purpose of Prototype
How to Profile JavaScript Execution
How to Get Evaluated Attributes Inside a Custom Directive
Check for Special Characters in String
Get First and Last Date of Current Month with JavaScript or Jquery
Escaping Strings in JavaScript
Smooth Scroll to Div Id Jquery
How to Prevent Your JavaScript Code from Being Stolen, Copied, and Viewed