Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> JavaScript

Xác thực URL Tutorialspoint bằng Regex JavaScript

Để xác thực một URL cụ thể, hãy sử dụng biểu thức chính quy (regular expression).

Ví dụ

Mã minh họa:

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"));

Để chạy chương trình, sử dụng lệnh:

node tenFile.js

Tên file ở đây là demo259.js.

Kết quả

Đầu ra trên console:

PS C:\Users\Amit\javascript-code> node demo259.js
index
java