ใช้ Regular Expression เพื่อตรวจสอบ URL เฉพาะเว็บไซต์
ตัวอย่างโค้ด
function validateSoundURL(myURL) {
var regularExpression = /^https?:\/\/(tutorialspoint\.com)\/(.*)$/;
return myURL.match(regularExpression) && myURL.match(regularExpression)[2];
}
console.log(validateSoundURL("https://tutorialspoint.com/index"));
console.log(validateSoundURL("https://tutorialspoint.com/java"));รันด้วยคำสั่ง:
node fileName.js
ชื่อไฟล์: demo259.js
ผลลัพธ์
PS C:\Users\Amit\javascript-code> node demo259.js index java
คำอธิบาย: Regex ^https?:\/\/(tutorialspoint\.com)\/(.*)$ ตรวจสอบโปรโตคอล http/https โดเมน tutorialspoint.com และจับกลุ่ม path หลังโดเมน ผลลัพธ์คือ path ที่จับได้