Skip to content

Instantly share code, notes, and snippets.

@ryasmi
Last active January 7, 2018 21:51
Show Gist options
  • Save ryasmi/25f177e5ca6341e16de8ef5cf95707a1 to your computer and use it in GitHub Desktop.
Save ryasmi/25f177e5ca6341e16de8ef5cf95707a1 to your computer and use it in GitHub Desktop.
escapedChar = (char) => {
return `\\${char}`;
}
normChars = `w_~!$&'()*+,;=:-`.split('').map(escapedChar).join('');
otherChars = '\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF\\uFFEF-\\uFFFD';
allChars = `${normChars}${otherChars}`;
extChars = `(?:[${allChars}]|(?:\\%[a-f0-9][a-f0-9]))`;
capturePattern = (pattern) => {
return `(${pattern})`;
}
delimitedPattern = (pattern, delimeter) => {
return `(?:${pattern})(?:${escapedChar(delimeter)}${pattern})*`;
};
prefixedPattern = (prefix, pattern) => {
return `${escapedChar(prefix)}(${pattern})`;
};
optionalPattern = (pattern) => {
return `(?:${pattern})?`;
};
scheme = `(?:(\\w+):\\/\\/)`;
authority = optionalPattern(capturePattern(delimitedPattern(`[${allChars}]+`, '.')));
path = optionalPattern(prefixedPattern('/', `(?:[\\.${allChars}]+\\/?)*`));
query = optionalPattern(prefixedPattern('?', `${extChars}*`));
fragment = optionalPattern(prefixedPattern('#', `${extChars}*`));
iri = new RegExp(`^${scheme}${authority}${path}${query}${fragment}$`, 'i');
x = 'http://www.example.com/fgdfgd?sdfsdfsdf#fgdgdfg';
iri.exec(x);
// If it's not a match, it'll return null.
// If it is a match, it'll return [fullString, scheme, authority, path, query, fragment].
const pattern = /^(?:(\w+):\/\/)(?:((?:[\w\_\~\!\$\&\'\(\)\*\+\,\;\=\:\-\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF\uFFEF-\uFFFD]+)(?:\.[\w\_\~\!\$\&\'\(\)\*\+\,\;\=\:\-\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF\uFFEF-\uFFFD]+)*))?(?:\/((?:[\.\w\_\~\!\$\&\'\(\)\*\+\,\;\=\:\-\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF\uFFEF-\uFFFD]+\/?)*))?(?:\?((?:[\w\_\~\!\$\&\'\(\)\*\+\,\;\=\:\-\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF\uFFEF-\uFFFD]|(?:\%[a-f0-9][a-f0-9]))*))?(?:\#((?:[\w\_\~\!\$\&\'\(\)\*\+\,\;\=\:\-\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF\uFFEF-\uFFFD]|(?:\%[a-f0-9][a-f0-9]))*))?$/i
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment