How to useLodash _.kebabCase() method in Javascript
Lodash _.kebabCase() method is used to convert the given string into a kebab case string. kebabCase is the practice of writing identifiers using hyphens instead of spaces. The string can be space-separated, dash-separated, or can be separated by underscores.
Syntax:
_.kebabCase([string='']);
Example: In this example, we are using Lodash _.kebabCase() method
const _ = require('lodash');
let str1 = _.kebabCase("GEEKS__FOR__GEEKS");
console.log(str1);
let str2 = _.kebabCase("GEEKS-----FOR_____Geeks");
console.log(str2);
let str3 = _.kebabCase("geeks--FOR--geeks");
console.log(str3);
Output:
"geeks-for-geeks"
"geeks-for-geeks"
"geeks-for-geeks"
How to convert a string into kebab case using JavaScript ?
Given a string with space-separated or camel case or snake case letters, the task is to find the kebab case of the following string.
Examples:
Input: Geeks For Geeks
Output: geeks-for-geeks
Input: w3wiki
Output: geeks-for-geeks
Input: Geeks_for_geeks
Output: geeks-for-geeks
Below are the approaches used to convert a string into a kebab case using JavaScript:
Table of Content
- Approach 1: Using the replace() method
- Approach 2: Using the match() method
- Approach 3: Using Lodash _.kebabCase() method
- Approach 4: Using for loop
Contact Us