0

I've seen some legendary questions about the topic like that: how to parse a url .

But years passed and things changed. The answers from questions that I can find is out of date. I don't want to parse URL via regexp or some hack like creating HTML node as a parse helper. I want some flexible method that returns an object with all required data from the URL. I believe that there are some new built-in methods to do it or new revolutionary amaizing and simple ES6 libraries for that purpose.

Can you please advice something like that?

Alex Belets
  • 356
  • 1
  • 4
  • 15
  • 2
    What makes you think this is going to be any better in ES6? What's you definition of "parse"? – Liam Jul 13 '18 at 15:13
  • Can you just share a new experience instead of simple downvote? – Alex Belets Jul 13 '18 at 15:13
  • @Liam I've seen new libraries (a huge amount). I'm sure that there is a nice solution. Better solution that every solution in the quoted question. – Alex Belets Jul 13 '18 at 15:15
  • Lose "Best, Best Practice," etc, provide sample input/output, and explain why this isn't a duplicate of the one you linked. – Kevin B Jul 13 '18 at 15:15
  • also bear in mind that [Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.](https://stackoverflow.com/help/on-topic) – Liam Jul 13 '18 at 15:16
  • 1
    Ok, I'll delete the question and'll re-create it with better description later. UPD. Oh, I cant :) – Alex Belets Jul 13 '18 at 15:16
  • @AlexBelets No, don't do that. If you're just going to recreate the question, see my answer below. – Brad Jul 13 '18 at 15:16
  • @Liam The question has nothing to do with recommended libraries and such, he has a fundamental misunderstanding of ES6. – Brad Jul 13 '18 at 15:17
  • @Brad the off site was in relation to [this comment](https://stackoverflow.com/questions/51328170/best-way-to-parse-url-in-es6#comment89631844_51328170) – Liam Jul 13 '18 at 15:18
  • @Brad it's not about ES6, it's about ES6 libraries in a ES6 context. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import – Alex Belets Jul 13 '18 at 15:21
  • 1
    @AlexBelets You're wrong about that, which is what I'm trying to explain to you. The fact that ES6 exists has absolutely nothing to do with your URL parsing. – Brad Jul 13 '18 at 15:22
  • 1
    So what do you want? Baring in mind, like I've already said, asking for libraries is off topic? If you want to know if this is part of ES6, brads already answered this, no it's not. – Liam Jul 13 '18 at 15:22

2 Answers2

14

I think you are looking for web api's URL() constructor like this:

const myTestURLString = "https://www.youtube.com:8080/watch?v=YaXXXXkt0Y&id=123";

const myURLObj = new URL(myTestURLString );

console.log(myURLObj.protocol);
console.log(myURLObj.host);
console.log(myURLObj.hostname);
console.log(myURLObj.pathname);
console.log(myURLObj.search);
console.log(myURLObj.searchParams.get('v'));
console.log(myURLObj.searchParams.get('id'));
BlackBeard
  • 10,246
  • 7
  • 52
  • 62
2

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

Brad
  • 159,648
  • 54
  • 349
  • 530
  • 2
    I didn't downvote, but I'm not really clear what your point is here? – Liam Jul 13 '18 at 15:17
  • 1
    @Liam The question assumes that specific API and frameworky features would be part of the ECMAScript specification, when reality is the language spec is deliberately designed to be generic and doesn't include a lot of these sorts of things. Therefore, any features added to ES6 aren't going to have anything to do with URL parsing. – Brad Jul 13 '18 at 15:19
  • 1
    I mean, that's great and all, but do you really believe an answer to that on a "best way to parse urls" question will be at all useful to the people coming here to parse urls? They'd be better off being auto-redirected to the duplicate. – Kevin B Jul 13 '18 at 15:22
  • @KevinB The title of the question isn't actually the question. In any case, I explain in my answer to use the URL interface in a browser context, and there are several compatible libraries for Node.js. – Brad Jul 13 '18 at 15:24
  • @Brad I've seen this specification. But there is a chance that I've missed something about new functionality around URL. It is possible. So thank you for clearing of this point. And thank you for an answer. I've missed this API. – Alex Belets Jul 13 '18 at 15:27