หากต้องการทดสอบสตริง URL ให้ใช้นิพจน์ทั่วไป
ตัวอย่าง
ต่อไปนี้เป็นรหัส -
var regularExpressionForURL = /^https?:\/\//i;
var originalURL1 = "https://www.example.com/index";
var originalURL2 = "/index";
if (regularExpressionForURL.test(originalURL1)) {
console.log("This is absolute URL");
}
else {
console.log("This is relative URL");
}
if (regularExpressionForURL.test(originalURL2)) {
console.log("This is absolute URL");
}
else {
console.log("This is relative URL");
} ในการรันโปรแกรมข้างต้น คุณต้องใช้คำสั่งต่อไปนี้ -
node fileName.js.
ที่นี่ ชื่อไฟล์ของฉันคือ demo266.js
ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้บนคอนโซล -
PS C:\Users\Amit\javascript-code> node demo266.js This is absolute URL This is relative URL