function slugify(text) | |
{ | |
return text.toString().toLowerCase() | |
.replace(/\s+/g, '-') // Replace spaces with - | |
.replace(/[^\w\-]+/g, '') // Remove all non-word chars | |
.replace(/\-\-+/g, '-') // Replace multiple - with single - | |
.replace(/^-+/, '') // Trim - from start of text | |
.replace(/-+$/, ''); // Trim - from end of text | |
} |
Ty my friend. Very usefull !
Continue with @wearecharette version, added a separator option if you want to use different slug separators.
slugify(text, separator) {
text = text.toString().toLowerCase().trim();
const sets = [
{to: 'a', from: '[ÀÁÂÃÄÅÆĀĂĄẠẢẤẦẨẪẬẮẰẲẴẶ]'},
{to: 'c', from: '[ÇĆĈČ]'},
{to: 'd', from: '[ÐĎĐÞ]'},
{to: 'e', from: '[ÈÉÊËĒĔĖĘĚẸẺẼẾỀỂỄỆ]'},
{to: 'g', from: '[ĜĞĢǴ]'},
{to: 'h', from: '[ĤḦ]'},
{to: 'i', from: '[ÌÍÎÏĨĪĮİỈỊ]'},
{to: 'j', from: '[Ĵ]'},
{to: 'ij', from: '[IJ]'},
{to: 'k', from: '[Ķ]'},
{to: 'l', from: '[ĹĻĽŁ]'},
{to: 'm', from: '[Ḿ]'},
{to: 'n', from: '[ÑŃŅŇ]'},
{to: 'o', from: '[ÒÓÔÕÖØŌŎŐỌỎỐỒỔỖỘỚỜỞỠỢǪǬƠ]'},
{to: 'oe', from: '[Œ]'},
{to: 'p', from: '[ṕ]'},
{to: 'r', from: '[ŔŖŘ]'},
{to: 's', from: '[ߌŜŞŠ]'},
{to: 't', from: '[ŢŤ]'},
{to: 'u', from: '[ÙÚÛÜŨŪŬŮŰŲỤỦỨỪỬỮỰƯ]'},
{to: 'w', from: '[ẂŴẀẄ]'},
{to: 'x', from: '[ẍ]'},
{to: 'y', from: '[ÝŶŸỲỴỶỸ]'},
{to: 'z', from: '[ŹŻŽ]'},
{to: '-', from: '[·/_,:;\']'}
];
sets.forEach(set => {
text = text.replace(new RegExp(set.from,'gi'), set.to);
});
text = text.toString().toLowerCase()
.replace(/\s+/g, '-') // Replace spaces with -
.replace(/&/g, '-and-') // Replace & with 'and'
.replace(/[^\w\-]+/g, '') // Remove all non-word chars
.replace(/\--+/g, '-') // Replace multiple - with single -
.replace(/^-+/, '') // Trim - from start of text
.replace(/-+$/, ''); // Trim - from end of text
if ((typeof separator !== 'undefined') && (separator !== '-')) {
text = text.replace(/-/g, separator);
}
return text;
}
thanks
Continue with @wearecharette version, added a separator option if you want to use different slug separators.
slugify(text, separator) { text = text.toString().toLowerCase().trim(); const sets = [ {to: 'a', from: '[ÀÁÂÃÄÅÆĀĂĄẠẢẤẦẨẪẬẮẰẲẴẶ]'}, {to: 'c', from: '[ÇĆĈČ]'}, {to: 'd', from: '[ÐĎĐÞ]'}, {to: 'e', from: '[ÈÉÊËĒĔĖĘĚẸẺẼẾỀỂỄỆ]'}, {to: 'g', from: '[ĜĞĢǴ]'}, {to: 'h', from: '[ĤḦ]'}, {to: 'i', from: '[ÌÍÎÏĨĪĮİỈỊ]'}, {to: 'j', from: '[Ĵ]'}, {to: 'ij', from: '[IJ]'}, {to: 'k', from: '[Ķ]'}, {to: 'l', from: '[ĹĻĽŁ]'}, {to: 'm', from: '[Ḿ]'}, {to: 'n', from: '[ÑŃŅŇ]'}, {to: 'o', from: '[ÒÓÔÕÖØŌŎŐỌỎỐỒỔỖỘỚỜỞỠỢǪǬƠ]'}, {to: 'oe', from: '[Œ]'}, {to: 'p', from: '[ṕ]'}, {to: 'r', from: '[ŔŖŘ]'}, {to: 's', from: '[ߌŜŞŠ]'}, {to: 't', from: '[ŢŤ]'}, {to: 'u', from: '[ÙÚÛÜŨŪŬŮŰŲỤỦỨỪỬỮỰƯ]'}, {to: 'w', from: '[ẂŴẀẄ]'}, {to: 'x', from: '[ẍ]'}, {to: 'y', from: '[ÝŶŸỲỴỶỸ]'}, {to: 'z', from: '[ŹŻŽ]'}, {to: '-', from: '[·/_,:;\']'} ]; sets.forEach(set => { text = text.replace(new RegExp(set.from,'gi'), set.to); }); text = text.toString().toLowerCase() .replace(/\s+/g, '-') // Replace spaces with - .replace(/&/g, '-and-') // Replace & with 'and' .replace(/[^\w\-]+/g, '') // Remove all non-word chars .replace(/\--+/g, '-') // Replace multiple - with single - .replace(/^-+/, '') // Trim - from start of text .replace(/-+$/, ''); // Trim - from end of text if ((typeof separator !== 'undefined') && (separator !== '-')) { text = text.replace(/-/g, separator); } return text; }
<3
Thanks
Continue with @wearecharette version, added a separator option if you want to use different slug separators.
slugify(text, separator) { text = text.toString().toLowerCase().trim(); const sets = [ {to: 'a', from: '[ÀÁÂÃÄÅÆĀĂĄẠẢẤẦẨẪẬẮẰẲẴẶ]'}, {to: 'c', from: '[ÇĆĈČ]'}, {to: 'd', from: '[ÐĎĐÞ]'}, {to: 'e', from: '[ÈÉÊËĒĔĖĘĚẸẺẼẾỀỂỄỆ]'}, {to: 'g', from: '[ĜĞĢǴ]'}, {to: 'h', from: '[ĤḦ]'}, {to: 'i', from: '[ÌÍÎÏĨĪĮİỈỊ]'}, {to: 'j', from: '[Ĵ]'}, {to: 'ij', from: '[IJ]'}, {to: 'k', from: '[Ķ]'}, {to: 'l', from: '[ĹĻĽŁ]'}, {to: 'm', from: '[Ḿ]'}, {to: 'n', from: '[ÑŃŅŇ]'}, {to: 'o', from: '[ÒÓÔÕÖØŌŎŐỌỎỐỒỔỖỘỚỜỞỠỢǪǬƠ]'}, {to: 'oe', from: '[Œ]'}, {to: 'p', from: '[ṕ]'}, {to: 'r', from: '[ŔŖŘ]'}, {to: 's', from: '[ߌŜŞŠ]'}, {to: 't', from: '[ŢŤ]'}, {to: 'u', from: '[ÙÚÛÜŨŪŬŮŰŲỤỦỨỪỬỮỰƯ]'}, {to: 'w', from: '[ẂŴẀẄ]'}, {to: 'x', from: '[ẍ]'}, {to: 'y', from: '[ÝŶŸỲỴỶỸ]'}, {to: 'z', from: '[ŹŻŽ]'}, {to: '-', from: '[·/_,:;\']'} ]; sets.forEach(set => { text = text.replace(new RegExp(set.from,'gi'), set.to); }); text = text.toString().toLowerCase() .replace(/\s+/g, '-') // Replace spaces with - .replace(/&/g, '-and-') // Replace & with 'and' .replace(/[^\w\-]+/g, '') // Remove all non-word chars .replace(/\--+/g, '-') // Replace multiple - with single - .replace(/^-+/, '') // Trim - from start of text .replace(/-+$/, ''); // Trim - from end of text if ((typeof separator !== 'undefined') && (separator !== '-')) { text = text.replace(/-/g, separator); } return text; }
it works! ;)
Wow everyone! what a great job making a great slugify function. Thanks!
Awesome thread folks!
Great job saved me a lot of time ! Thanks everyone :)
Continue with @wearecharette version, added a separator option if you want to use different slug separators.
slugify(text, separator) { text = text.toString().toLowerCase().trim(); const sets = [ {to: 'a', from: '[ÀÁÂÃÄÅÆĀĂĄẠẢẤẦẨẪẬẮẰẲẴẶ]'}, {to: 'c', from: '[ÇĆĈČ]'}, {to: 'd', from: '[ÐĎĐÞ]'}, {to: 'e', from: '[ÈÉÊËĒĔĖĘĚẸẺẼẾỀỂỄỆ]'}, {to: 'g', from: '[ĜĞĢǴ]'}, {to: 'h', from: '[ĤḦ]'}, {to: 'i', from: '[ÌÍÎÏĨĪĮİỈỊ]'}, {to: 'j', from: '[Ĵ]'}, {to: 'ij', from: '[IJ]'}, {to: 'k', from: '[Ķ]'}, {to: 'l', from: '[ĹĻĽŁ]'}, {to: 'm', from: '[Ḿ]'}, {to: 'n', from: '[ÑŃŅŇ]'}, {to: 'o', from: '[ÒÓÔÕÖØŌŎŐỌỎỐỒỔỖỘỚỜỞỠỢǪǬƠ]'}, {to: 'oe', from: '[Œ]'}, {to: 'p', from: '[ṕ]'}, {to: 'r', from: '[ŔŖŘ]'}, {to: 's', from: '[ߌŜŞŠ]'}, {to: 't', from: '[ŢŤ]'}, {to: 'u', from: '[ÙÚÛÜŨŪŬŮŰŲỤỦỨỪỬỮỰƯ]'}, {to: 'w', from: '[ẂŴẀẄ]'}, {to: 'x', from: '[ẍ]'}, {to: 'y', from: '[ÝŶŸỲỴỶỸ]'}, {to: 'z', from: '[ŹŻŽ]'}, {to: '-', from: '[·/_,:;\']'} ]; sets.forEach(set => { text = text.replace(new RegExp(set.from,'gi'), set.to); }); text = text.toString().toLowerCase() .replace(/\s+/g, '-') // Replace spaces with - .replace(/&/g, '-and-') // Replace & with 'and' .replace(/[^\w\-]+/g, '') // Remove all non-word chars .replace(/\--+/g, '-') // Replace multiple - with single - .replace(/^-+/, '') // Trim - from start of text .replace(/-+$/, ''); // Trim - from end of text if ((typeof separator !== 'undefined') && (separator !== '-')) { text = text.replace(/-/g, separator); } return text; }
superb
Thank you for these suggestions!
I personally prefer to replace æ
and Æ
by ae
instead of a
.
You can do this by replacing:
{to: 'a', from: '[ÀÁÂÃÄÅÆĀĂĄẠẢẤẦẨẪẬẮẰẲẴẶ]'},
with:
{to: 'a', from: '[ÀÁÂÃÄÅĀĂĄẠẢẤẦẨẪẬẮẰẲẴẶ]'}, // note the Æ removing
{to: 'ae', from: '[Æ]'},
Also, I think the first .toLowerCase()
is not necessary, since the regex performs a case-insensitive search.
Seems this is not working with Chinese characters: 商务计划模板
Replaced:
.replace(/[^\w\-]+/g, '') // Remove all non-word chars
with
.replace(/[^a-zA-Z0-9_\u3400-\u9FBF\s-]/g, '') // Remove all non-word chars
Also have a look at this:
https://stackoverflow.com/questions/25698733/how-to-generate-url-slug-from-chinese-characters
Thanks, bro!
Super Cool! Thank you!
It works fine. Thank you!
The last one mentioned by @jdcookie is very cool!
Thank you :)
Thanks man : )
Thanks.
Thank you!
thanks for this!
In Norwegian "Å" is also often replaced width aa. In order to do this we need to include:
{to: 'aa', from: '[a°]'}
Somehow "å" becomes the sequence: "a" followed by degree "°"
For better support for German, here's a modification on top of @eusonlito's script with the expected mappings of umlauts and ß (ä
-> ae
, ß
-> ss
, etc):
function slugify(text, separator) {
text = text.toString().toLowerCase().trim()
const sets = [
{ to: "a", from: "[ÀÁÂÃÅÆĀĂĄẠẢẤẦẨẪẬẮẰẲẴẶ]" },
{ to: "ae", from: "[Ä]" },
{ to: "c", from: "[ÇĆĈČ]" },
{ to: "d", from: "[ÐĎĐÞ]" },
{ to: "e", from: "[ÈÉÊËĒĔĖĘĚẸẺẼẾỀỂỄỆ]" },
{ to: "g", from: "[ĜĞĢǴ]" },
{ to: "h", from: "[ĤḦ]" },
{ to: "i", from: "[ÌÍÎÏĨĪĮİỈỊ]" },
{ to: "j", from: "[Ĵ]" },
{ to: "ij", from: "[IJ]" },
{ to: "k", from: "[Ķ]" },
{ to: "l", from: "[ĹĻĽŁ]" },
{ to: "m", from: "[Ḿ]" },
{ to: "n", from: "[ÑŃŅŇ]" },
{ to: "o", from: "[ÒÓÔÕØŌŎŐỌỎỐỒỔỖỘỚỜỞỠỢǪǬƠ]" },
{ to: "oe", from: "[΅]" },
{ to: "p", from: "[ṕ]" },
{ to: "r", from: "[ŔŖŘ]" },
{ to: "s", from: "[ŚŜŞŠ]" },
{ to: "ss", from: "[ß]" },
{ to: "t", from: "[ŢŤ]" },
{ to: "u", from: "[ÙÚÛŨŪŬŮŰŲỤỦỨỪỬỮỰƯ]" },
{ to: "ue", from: "[Ü]" },
{ to: "w", from: "[ẂŴẀẄ]" },
{ to: "x", from: "[ẍ]" },
{ to: "y", from: "[ÝŶŸỲỴỶỸ]" },
{ to: "z", from: "[ŹŻŽ]" },
{ to: "-", from: "[·/_,:;']" },
]
sets.forEach((set) => {
text = text.replace(new RegExp(set.from, "gi"), set.to)
})
text = text
.toString()
.toLowerCase()
.replace(/\s+/g, "-") // Replace spaces with -
.replace(/&/g, "-and-") // Replace & with 'and'
.replace(/[^\w\-]+/g, "") // Remove all non-word chars
.replace(/\--+/g, "-") // Replace multiple - with single -
.replace(/^-+/, "") // Trim - from start of text
.replace(/-+$/, "") // Trim - from end of text
if (typeof separator !== "undefined" && separator !== "-") {
text = text.replace(/-/g, separator)
}
return text
}
Nice thanks for sharing
Thanks a lot!
Extending @jdahdah 's version with support for Greek
function slugify(text, separator) {
text = text.toString().toLowerCase().trim()
const sets = [
{to: "a", from: "[ÀÁÂÃÅÆĀĂĄẠẢẤẦẨẪẬẮẰẲẴẶΑΆ]"},
{to: "b", from: "[Β]"},
{to: "ae", from: "[Ä]"},
{to: "c", from: "[ÇĆĈČ]"},
{to: "d", from: "[ÐĎĐÞΔ]"},
{to: "e", from: "[ÈÉÊËĒĔĖĘĚẸẺẼẾỀỂỄỆΕΈ]"},
{to: "f", from: "[Φ]"},
{to: "g", from: "[ĜĞĢǴΓ]"},
{to: "h", from: "[ĤḦ]"},
{to: "i", from: "[ÌÍÎÏĨĪĮİỈỊΗΉΙΊΪΐ]"},
{to: "j", from: "[Ĵ]"},
{to: "ij", from: "[IJ]"},
{to: "k", from: "[ĶΚ]"},
{to: "ks", from: "[Ξ]"},
{to: "l", from: "[ĹĻĽŁΛ]"},
{to: "m", from: "[ḾΜ]"},
{to: "n", from: "[ÑŃŅŇΝ]"},
{to: "o", from: "[ÒÓÔÕØŌŎŐỌỎỐỒỔỖỘỚỜỞỠỢǪǬƠΟΌΩΏ]"},
{to: "oe", from: "[΅]"},
{to: "p", from: "[ṕΠ]"},
{to: "ps", from: "[Ψ]"},
{to: "r", from: "[ŔŖŘΡ]"},
{to: "s", from: "[ŚŜŞŠΣς]"},
{to: "ss", from: "[ß]"},
{to: "t", from: "[ŢŤΤ]"},
{to: "th", from: "[Θ]"},
{to: "u", from: "[ÙÚÛŨŪŬŮŰŲỤỦỨỪỬỮỰƯΥΎΫΰ]"},
{to: "ue", from: "[Ü]"},
{to: "w", from: "[ẂŴẀẄ]"},
{to: "x", from: "[ẍΧ]"},
{to: "y", from: "[ÝŶŸỲỴỶỸ]"},
{to: "z", from: "[ŹŻŽΖ]"},
{to: "-", from: "[·/_,:;']"},
]
sets.forEach((set) => {
text = text.replace(new RegExp(set.from, "gi"), set.to)
})
text = text
.toString()
.toLowerCase()
.replace(/\s+/g, "-") // Replace spaces with -
.replace(/&/g, "-and-") // Replace & with 'and'
.replace(/[^\w\-]+/g, "") // Remove all non-word chars
.replace(/\--+/g, "-") // Replace multiple - with single -
.replace(/^-+/, "") // Trim - from start of text
.replace(/-+$/, "") // Trim - from end of text
if(typeof separator !== "undefined" && separator !== "-") {
text = text.replace(/-/g, separator)
}
return text
}
Thanks a lot!
For better support for German, here's a modification on top of @eusonlito's script with the expected mappings of umlauts and ß (
ä
->ae
,ß
->ss
, etc):function slugify(text, separator) { text = text.toString().toLowerCase().trim() const sets = [ { to: "a", from: "[ÀÁÂÃÅÆĀĂĄẠẢẤẦẨẪẬẮẰẲẴẶ]" }, { to: "ae", from: "[Ä]" }, { to: "c", from: "[ÇĆĈČ]" }, { to: "d", from: "[ÐĎĐÞ]" }, { to: "e", from: "[ÈÉÊËĒĔĖĘĚẸẺẼẾỀỂỄỆ]" }, { to: "g", from: "[ĜĞĢǴ]" }, { to: "h", from: "[ĤḦ]" }, { to: "i", from: "[ÌÍÎÏĨĪĮİỈỊ]" }, { to: "j", from: "[Ĵ]" }, { to: "ij", from: "[IJ]" }, { to: "k", from: "[Ķ]" }, { to: "l", from: "[ĹĻĽŁ]" }, { to: "m", from: "[Ḿ]" }, { to: "n", from: "[ÑŃŅŇ]" }, { to: "o", from: "[ÒÓÔÕØŌŎŐỌỎỐỒỔỖỘỚỜỞỠỢǪǬƠ]" }, { to: "oe", from: "[ŒÖ]" }, { to: "p", from: "[ṕ]" }, { to: "r", from: "[ŔŖŘ]" }, { to: "s", from: "[ŚŜŞŠ]" }, { to: "ss", from: "[ß]" }, { to: "t", from: "[ŢŤ]" }, { to: "u", from: "[ÙÚÛŨŪŬŮŰŲỤỦỨỪỬỮỰƯ]" }, { to: "ue", from: "[Ü]" }, { to: "w", from: "[ẂŴẀẄ]" }, { to: "x", from: "[ẍ]" }, { to: "y", from: "[ÝŶŸỲỴỶỸ]" }, { to: "z", from: "[ŹŻŽ]" }, { to: "-", from: "[·/_,:;']" }, ] sets.forEach((set) => { text = text.replace(new RegExp(set.from, "gi"), set.to) }) text = text .toString() .toLowerCase() .replace(/\s+/g, "-") // Replace spaces with - .replace(/&/g, "-and-") // Replace & with 'and' .replace(/[^\w\-]+/g, "") // Remove all non-word chars .replace(/\--+/g, "-") // Replace multiple - with single - .replace(/^-+/, "") // Trim - from start of text .replace(/-+$/, "") // Trim - from end of text if (typeof separator !== "undefined" && separator !== "-") { text = text.replace(/-/g, separator) } return text }
Thank you a lot
@anhtuank7c I think we don't need this part.
if (typeof separator !== "undefined" && separator !== "-") {
text = text.replace(/-/g, separator)
}
Just insert it in replace chain above
...
.replace(/\s+/g, separator || "-")
...
thanks brotha.