- Instant help with your JavaScript coding problems

Convert string to kebab case in JavaScript

Question:
How to convert string to kebab case in JavaScript?
Answer:
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
Description:

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?"
Related snippets:
Tags:
kebab case, hyphen case, dash, hyphen, convert, string, javascript
Technical term:
Convert string to kebab case in JavaScript