Created
July 12, 2012 16:49
-
-
Save zaach/3099261 to your computer and use it in GitHub Desktop.
origin regex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//var org = /^https?:\/\/(?=.{1,254}(?::|$))(?:(?!\d|-)(?![a-zA-Z0-9\-]{1,62}-\.)[a-zA-Z0-9\-]{1,63}\.)*(?:(?!\d|-)(?![a-zA-Z0-9\-]{1,62}-$)[a-zA-Z0-9\-]{1,63})(:\d+)?$/; | |
var org = /^https?:\/\/(?=.{1,254}(?::|$))(?:(?!\d|-)(?![a-z0-9\-]{1,62}-(?:\.|:|$))[a-z0-9\-]{1,63}\b(?!\.$)\.?)+(:\d+)?$/i; | |
/* Origin regex explained | |
/^ // beginning | |
https?:\/\/ // starts with http:// or https:// | |
(?=.{1,254}(?::|$)) // the hostname chars must be within 1-254 bytes | |
// match one or more parts of a hostname (<part>.<part>) | |
(?:(?!\d|-) // cannot start with a digit or dash | |
(?![a-z0-9\-]{1,62}- // part cannot end with a dash | |
(?:\.|:|$)) // (end of part will be '.', ':', or end of str) | |
[a-z0-9\-]{1,63}\b // part will be 1-63 letters, numbers, or dashes | |
(?!\.$) // last part cannot end with a '.' | |
\.? // part followed by '.' unless the last one | |
)+ // one or more hostname parts | |
(:\d+)? // optional port | |
$/i; // end; case-insensitive | |
*/ | |
console.log("good"); | |
console.log("https://google.com".match(org)); // good | |
console.log("https://hey.blahahahah.google.com".match(org)); // good | |
console.log("https://hey.blah-ahahah.google.com".match(org)); // good | |
console.log("https://localhost".match(org)); // good | |
console.log("https://local.host:8008".match(org)); // good | |
console.log("https://local-host".match(org)); // good | |
console.log("https://hey.blahahahah.google.c".match(org)); // good | |
console.log("https://aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:1002".match(org)); // hostname short enough, port not included | |
console.log("\nbad should be null:"); | |
console.log("https://local.host:8008a".match(org)); // bad port | |
console.log("https://-hey.blah-ahahah.google.com".match(org)); // starts with dash | |
console.log("https://hey.blah-ahahah.google-.com".match(org)); // part ends with dash | |
console.log("https://0hey.blah-ahahah.google.com".match(org)); // starts with number | |
console.log("https://localhost-".match(org)); // ends with dash | |
console.log("https://localhost.".match(org)); // no final part | |
console.log("https://".match(org)); // no hostname | |
console.log("https://aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-ahahah.google.com".match(org)); // label too long | |
console.log("https://aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa--hahah.google.com".match(org)); // label too long | |
console.log("https://aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-0hahah.google.com".match(org)); // label too long | |
console.log("https://aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".match(org)); // hostname too long | |
console.log("https://aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:1002".match(org)); // hostname too long, with port |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment