Problem
Current isValidURL regex may not handle:
localhost
localhost:3000
- IPv6 addresses
- URLs with authentication (
user:pass@host)
- Data URLs
Current Implementation
var pattern = new RegExp('^(https?:\\/\\/)?' +
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' +
'((\\d{1,3}\\.){3}\\d{1,3}))' +
'(\\:\\d+)?(\\/[-a-z\\d%_.=~+!]*)*' +
'(\\?[;&a-z\\d%_.~+=/-]*)?' +
'(\\#[-a-z\\d_]*)?$', 'i');
Suggested Fix
Use the URL constructor for validation:
isValidURL: {
value: function (url) {
try {
new URL(url);
return true;
} catch {
return false;
}
}
}
Test Cases That Should Pass
http://localhost
http://localhost:3000/api
http://[::1]:8080
https://example.com
Problem
Current
isValidURLregex may not handle:localhostlocalhost:3000user:pass@host)Current Implementation
Suggested Fix
Use the URL constructor for validation:
Test Cases That Should Pass
http://localhosthttp://localhost:3000/apihttp://[::1]:8080https://example.com