Skip to content

Instantly share code, notes, and snippets.

@jesusalber1
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jesusalber1/2bd949ec1ece0b1407a8 to your computer and use it in GitHub Desktop.
Save jesusalber1/2bd949ec1ece0b1407a8 to your computer and use it in GitHub Desktop.
Sanitize filename
function sanitizeFileName(name) {
return name
.replace(/ /g, '-') /* Spaces as - (slug...) */
.replace(/[^A-Za-z0-9-_\.]/g, '') /* Only letters, numbers and symbols: - _ . */
.replace(/\.+/g, '.') /* .. or more -> . */
.replace(/-+/g, '-') /* -- or more -> - */
.replace(/_+/g, '_'); /* __ or more -> _ */
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment