How to parse a URL?
Please note that this solution is not the best. I made this just to match the requirements of the OP. I personally would suggest looking into the other answers.
THe following regexp will give you back the domain and the rest. :\/\/(.[^\/]+)(.*)
:
- www.google.com
- /goosomething
I suggest you studying the RegExp documentation here: http://www.regular-expressions.info/reference.html
Using your function:
function get_domain_name()
{
aaaa="http://www.somesite.se/blah/sdgsdgsdgs";
//aaaa="http://somesite.se/blah/sese";
var matches = aaaa.match(/:\/\/(?:www\.)?(.[^/]+)(.*)/);
alert(matches[1]);
alert(matches[2]);
}
How do I parse a URL into hostname and path in javascript?
var getLocation = function(href) {
var l = document.createElement("a");
l.href = href;
return l;
};
var l = getLocation("http://example.com/path");
console.debug(l.hostname)
>> "example.com"
console.debug(l.pathname)
>> "/path"
How to parse a URL and extract the required substring
I'd do it this way:
require 'uri'
uri = URI.parse('http://something.example.com/directory/')
uri.host.split('.').first
=> "something"
URI is built into Ruby. It's not the most full-featured but it's plenty capable of doing this task for most URLs. If you have IRIs then look at Addressable::URI.
How can I parse HTTP urls in C#?
I think you can get a lot of use out of the System.Uri class. Feed it a URI and you can pull out pieces in a number of arrangements.
Some examples:
Uri myUri = new Uri("http://server:8080/func2/SubFunc2?query=somevalue");
// Get host part (host name or address and port). Returns "server:8080".
string hostpart = myUri.Authority;
// Get path and query string parts. Returns "/func2/SubFunc2?query=somevalue".
string pathpart = myUri.PathAndQuery;
// Get path components. Trailing separators. Returns { "/", "func2/", "sunFunc2" }.
string[] pathsegments = myUri.Segments;
// Get query string. Returns "?query=somevalue".
string querystring = myUri.Query;
Elegant way parsing URL
Using the URI class you can do this:
var url = new Uri("your url");
How To Parse URL in Deno
No external module is needed to parse URLs in Deno. The URL
class is available as a global, just like in your browser:
const urlString = "https://www.google.com";
const url = new URL(urlString);
console.log(`URL: ${url.protocol}//${url.host}`);
How to parse URI gracefully using Go?
Use net/url
library
kafkaUrl := "kafka://user@10.10.172.222:9092?mechanism=PLAIN&protocol=SASL_PLAINTEXT"
u, err := url.Parse(kafkaUrl)
if err != nil {
// handle error
}
user := u.User.Username()
pass, isPassSet := u.User.Password()
host := u.Host // host or host:port
hostname and port separetely
hostname := u.Hostname()
port := u.Port()
Best way to parse URL in ES6
ES6 is part of the language specification, not any particular framework for JavaScript. Therefore, you're not going to find things like URL.parse() in the language.
The APIs you're looking for are part of the host for your application, such as the browser or Node.js. In browser, there is a URL interface: https://developer.mozilla.org/en-US/docs/Web/API/URL
Related Topics
How to Verify Jquery Ajax Events with Jasmine
How to Remove Emoji Code Using JavaScript
Input File to Array JavaScript/Jquery
How to Serve a File for Download with Angularjs or JavaScript
Call a Vue.Js Component Method from Outside the Component
Convert String Array Representation Back to an Array
How to Set Image Source with Base64
How to Read a Text File from Server Using JavaScript
Where Would I Use a Bitwise Operator in JavaScript
How to Request the Garbage Collector in Node.Js to Run
$.Each() VS For() Loop - and Performance
How Does the Messages-Count Example in Meteor Docs Work
What Does the @ Symbol Do in JavaScript Imports
Creating a JavaScript Cookie on a Domain and Reading It Across Sub Domains