- Instant help with your JavaScript coding problems

Convert string to kebab case in JavaScript

How to convert string to kebab case in JavaScript?
function toKebabCase(str){
    return str.toLowerCase().replace(/[^a-zA-Z0-9]+(.)/g, (m, chr) => '-' + chr).trim();

console.log(toKebabCase('the name field id  őúáűí   is name-field_id')); // the-name-field-id-is-name-field-id

In website URLs, you can often see that the path string is a multiple-word phrase separated by hyphen - characters. Sometimes it is referenced as SEO friendly dash delimited string like this: this-is-my-text. This formatting is called the kebab case


Share "How to convert string to kebab case in JavaScript?"