-2

How to match just the root of a URL and not the whole URL from a text string

I found this code but it does not work what is the missing?

                $("#divMenu")["append"]("<ul class='myClassId'><li><a href='" + arrLinks[i] + "'><img src='https://www.google.com/s2/favicons?domain=" + tempLink + "'/>" + arrLinks[i]["split"]("//")[1] + "<i class='fa fa-angle-right'></i></a></li></ul>");




https://www.example.com/12xy45
https://example.com/random
http://www.example.com/12xy45
http://example.com/random

resolving to the 

www.example.com/12xy45
example.com/random
www.example.com/12xy45
example.com/random

Resolving Not root URL only example: example.com domain.

Octavia
  • 43
  • 8
  • 1
    Possible duplicate of [How do I parse a URL into hostname and path in javascript?](https://stackoverflow.com/questions/736513/how-do-i-parse-a-url-into-hostname-and-path-in-javascript) – Dai Apr 09 '18 at 04:19

1 Answers1

0

use regex.

var uri = 'https://www.example.com/12xy45';

console.log(uri.replace(/^https?\:\/\//i, ""));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Albert Einstein
  • 7,472
  • 8
  • 36
  • 71