Để kiểm tra chuỗi URL, hãy sử dụng biểu thức chính quy.
Ví dụ
Sau đây là mã -
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");
} Để chạy chương trình trên, bạn cần sử dụng lệnh sau -
node fileName.js.
Đây, tên tệp của tôi là demo266.js
Đầu ra
Điều này sẽ tạo ra kết quả sau trên bảng điều khiển -
PS C:\Users\Amit\javascript-code> node demo266.js This is absolute URL This is relative URL