URL स्ट्रिंग से होस्टनाम निकालने के लिए, स्प्लिट () फ़ंक्शन का उपयोग करें। निम्नलिखित कोड है -
उदाहरण
function gettingTheHostNameFromURL(websiteURL) {
var getTheHostName;
if (websiteURL.indexOf("//") > -1) {
getTheHostName = websiteURL.split('/')[2];
} else {
getTheHostName = websiteURL.split('/')[0];
}
getTheHostName = getTheHostName.split(':')[0];
getTheHostName = getTheHostName.split('?')[0];
return getTheHostName;
}
var websiteURL="https://www.tutorialspoint.com/java/index.htm";var
websiteURL1="https://www.tutorix.com/about_us.htm";
var websiteURL2="https://www.tutorialspoint.com/execute_python_online.php";
console.log(gettingTheHostNameFromURL(websiteURL))
console.log(gettingTheHostNameFromURL(websiteURL1))
console.log(gettingTheHostNameFromURL(websiteURL2)) उपरोक्त प्रोग्राम को चलाने के लिए, आपको निम्न कमांड का उपयोग करने की आवश्यकता है -
node fileName.js.
आउटपुट
यहाँ, मेरी फ़ाइल का नाम है demo161.js। यह निम्नलिखित आउटपुट देगा -
PS C:\Users\Amit\JavaScript-code> node demo161.js www.tutorialspoint.com www.tutorix.com www.tutorialspoint.com