Skip to content

strToSnakeCase

Converts a string to snake_case: every word is lowercased and joined with an underscore.

The string is split with words, so spaces, punctuation, - and _ all act as delimiters, camelCase boundaries split, an acronym is separated from the word after it (XMLHttpRequest becomes xml_http_request) and a run of digits is its own word (abc12def becomes abc_12_def).

Scripts without upper and lower case keep their characters but still take part in the split, so 한글English혼합 becomes 한글_english_혼합.

This is the counterpart of strToCamelCase, strToPascalCase, strToKebabCase and strToConstantCase.

Parameters

NameTypeRequiredDefault
textstringStringstr
The string to convert. An empty or missing value returns an empty string.

Returns

string

String

str

Examples

javascript
strToSnakeCase('foo bar'); // Returns 'foo_bar'
strToSnakeCase('--foo-bar--'); // Returns 'foo_bar'
strToSnakeCase('camelCase'); // Returns 'camel_case'
strToSnakeCase('XMLHttpRequest'); // Returns 'xml_http_request'
strToSnakeCase('abc12def'); // Returns 'abc_12_def'
dart
strToSnakeCase('foo bar'); // Returns 'foo_bar'
strToSnakeCase('--foo-bar--'); // Returns 'foo_bar'
strToSnakeCase('camelCase'); // Returns 'camel_case'
strToSnakeCase('XMLHttpRequest'); // Returns 'xml_http_request'
strToSnakeCase('abc12def'); // Returns 'abc_12_def'
python
strToSnakeCase('foo bar')  # Returns 'foo_bar'
strToSnakeCase('--foo-bar--')  # Returns 'foo_bar'
strToSnakeCase('camelCase')  # Returns 'camel_case'
strToSnakeCase('XMLHttpRequest')  # Returns 'xml_http_request'
strToSnakeCase('abc12def')  # Returns 'abc_12_def'

Released under the MIT License