Slugify
JavaScript
// - should group the regexDelimiter in case there is more than one character (see last example)
// Potential problem when the delimiter is more than 1 character. Is the fix as simple as this:
// new RegExp("([^a-z0-9" + regexDelimiter + "])", "g")
// new RegExp("([^a-z0-9]|(?:" + regexDelimiter + "))", "g")
// - not sure if all the default separators are appropriate
// - investigate if it is worth to re-order the items in the sanitizer array
// - chain all the operations within the return statement
// - are the currency replacements are appropriate?
// - add a 3rd optional parameter to truncate after a certain character limit (truncat to the nearest delimiter
/**
* Converts a string to a "URL-safe" slug.
* Allows for some customization with two optional parameters:
*
* @param {string} Delimiter used. If not specified, defaults to a dash "-"
* @param {array} Adds to the list of non-alphanumeric characters which
* will be converted to the delimiter. The default list includes:
* ['–', '—', '―', '~', '\\', '/', '|', '+', '\'', '‘', '’', ' ']
*/
if (!String.prototype.slugify) {
String.prototype.slugify = function (delimiter, separators) {
var i = separators && separators.length,
slug = this,
delimiter = delimiter || '-',
regexEscape = new RegExp(/[[\/\\^$*+?.()|{}\]]/g),
regexDelimiter = delimiter.replace(regexEscape, "\\$&"),
prohibited = new RegExp("([^a-z0-9" + regexDelimiter + "])", "g"),
consecutive = new RegExp("(" + regexDelimiter + "+)", "g"),
trim = new RegExp("^" + regexDelimiter + "*(.*?)" + regexDelimiter + "*$"),
sanitizer = {
// common latin
'á': 'a',
'à': 'a',
'â': 'a',
'ä': 'a',
'ã': 'a',
'æ': 'ae',
'ç': 'c',
'é': 'e',
'è': 'e',
...