Skip to content
Home » Program to Convert String to Snake Case in JavaScript

Program to Convert String to Snake Case in JavaScript

In this blog post, we will discuss a JavaScript program that converts a given string to snake case. Snake case is a popular naming convention in programming where each word in the string is separated by an underscore. The program will take a string as input and convert it to snake case using a simple algorithm. This program can be useful in scenarios where we need to convert strings from one naming convention to another.

Code

const toSnakeCase = str =>
  str &&
  str
    .match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
    .map(x => x.toLowerCase())
    .join('_');

console.log(toSnakeCase('camelCase'));
console.log(toSnakeCase('some text'));
console.log(toSnakeCase('some-mixed_string With spaces_underscores-and-hyphens'));
console.log(toSnakeCase('AllThe-small Things'));
console.log(toSnakeCase('IAmListeningToFMWhileLoadingDifferentURLOnMyBrowserAndAlsoEditingSomeXMLAndHTML'));

Code Explanation

The provided code snippet demonstrates the implementation of the toSnakeCase function in JavaScript. Let’s walk through the code and understand how it works.

javascript
const toSnakeCase = str =>
  str &&
  str
    .match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
    .map(x => x.toLowerCase())
    .join('_');

console.log(toSnakeCase('camelCase'));
console.log(toSnakeCase('some text'));
console.log(toSnakeCase('some-mixed_string With spaces_underscores-and-hyphens'));
console.log(toSnakeCase('AllThe-small Things'));
console.log(toSnakeCase('IAmListeningToFMWhileLoadingDifferentURLOnMyBrowserAndAlsoEditingSomeXMLAndHTML'));

The code snippet defines a function named toSnakeCase that takes a string (str) as input. The function uses a combination of regex and array manipulation to convert the string to snake case.

1. The str.match(...) method is used to match and extract separate words from the input string. The regex pattern [A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+ is used to handle different cases such as camel case, words with numbers, acronyms, etc.

2. The .map(...) method is then used to convert each matched word to lowercase.

3. Finally, the .join('_') method is applied to join the lowercase words with an underscore, resulting in the snake case representation of the input string.

The program uses the console.log(...) method to demonstrate the usage of the toSnakeCase function with different test cases.

Output:

camel_case
some_text
some_mixed_string_with_spaces_underscores_and_hyphens
all_the_small_things
i_am_listening_to_fm_while_loading_different_url_on_my_browser_and_also_editing_some_xml_and_html

Conclusion

In this blog post, we explored a JavaScript program that converts a given string to snake case using a regex-based approach. This program can be useful in scenarios where we need to transform string naming conventions. By following the explanations and examples provided, you can easily implement and utilize the `toSnakeCase` function in your JavaScript projects.

Also checkout the following codes.


Program to Print “Hello” using JavaScript
How to Implement Table Export Options using BootstrapTable Library