Skip to content

Instantly share code, notes, and snippets.

@triptych
Forked from razass/sanitizeUrl.js
Created September 21, 2010 21:19
Show Gist options
  • Save triptych/590575 to your computer and use it in GitHub Desktop.
Save triptych/590575 to your computer and use it in GitHub Desktop.
/**
* Sanitize URL — Validate it looks like a URL, then make it less dirty.
*
* Oddnut Software
* Copyright (c) 2010 Eric Ferraiuolo - http://eric.ferraiuolo.name
* MIT License - http://www.opensource.org/licenses/mit-license.php
*
* Examples:
*
* 'Http://WWW.example.com/' » 'http://www.example.com/'
* 'example.com' » 'http://example.com'
* 'example.c' » null
* 'https://foo:[email protected]' » 'https://foo:[email protected]'
* ' WWW.EXAMPLE.COM ' » 'http://www.example.com'
*/
var sanitizeUrl = (function(){
var URL_REGEXP = /^(?:(https?:)\/\/)?(?:([^:@\s]+:?[^:@\s]+?)@)?((?:[^;:@=\/\?\.\s]+\.)+[A-Za-z0-9\-]{2,})(?::(\d+))?(?=\/|$)(\/.*)?/i,
TRIM_REGEXP = /^\s+|\s+$/g,
HTTP_PROTOCOL = 'http:',
SLASH_SLASH = '//',
AT = '@',
COLON = ':',
EMPTY_STRING = '';
function trim (s) {
try {
return s.replace(TRIM_REGEXP, EMPTY_STRING);
} catch(e) {
return s;
}
}
return function (url) {
url = trim(url);
if (url.length < 1) { return null; }
var urlParts = url.match(URL_REGEXP),
protocol, authorization, host, port, path;
if ( ! urlParts) { return null; }
protocol = (urlParts[1] ? urlParts[1].toLowerCase() : HTTP_PROTOCOL) + SLASH_SLASH;
authorization = (urlParts[2] ? (urlParts[2] + AT) : EMPTY_STRING);
host = (urlParts[3] ? urlParts[3].toLowerCase() : EMPTY_STRING);
port = (urlParts[4] ? (COLON + urlParts[4]) : EMPTY_STRING);
path = (urlParts[5] || EMPTY_STRING);
return ( protocol + authorization + host + port + path );
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment