Problem: You are given a host name, and from it you want to replace host name of one website with another which follows the exact Url pattern
eg: Url = https://github.com/kurianbenoy-aot/Learning
In the above url we need to replace github with gitlab because I have a clone repo in gitlab also with same domain name.
Url = "https://github.com/kurianbenoy-aot/Learning" replaceUrl = "https://gitlab.com" Urlextract= (Url.split('://')).split('/') replaceUrlextract = replaceUrl.split("//") //answer is: "https://gitlab.com/kurianbenoy-aot/Learning" console.log(Url.replace(Urlextract, replaceUrlextract))
What if we just wanted to extract the domain name from Url:
ans = Url.replace("https://", "").replace("http://", "").split('/') console.log(ans)