How to extract domain from a String in JS so for each String in the below list the output will be example.com except the last two where the output should be null or undefined or empty string. I am basically just trying to extract a domain from a string and below are the test cases to verify it.
var urls = [
"case 1 http://example.com",
"case 2 https://example.com",
"case 3 custume_scheme://example.com",
"case 4 www.example.com",
"case 5 www.example.com/staffToIgnore",
"case 6 www.example.com?=key=leyToIgnore",
"case 7 www.example.com ignore all those too",
"case 8 www.example.com www.example2.com",
"case 9 example.com need to return null",
"case 10 wwwa.example.com need to return null",
];
- The extension of the domain could be other things then
.com
, it could be anything from the form [a-z0-9] - sub-domains allowed.
There been several similar question to this one, but non of them as specific and non of the answers pass all the cases here.