Converts a string to snake casing. For example, 'hello world' becomes 'hello_world'.
Contributed by @itsbrunodev
function toSnakeCase(str) {
return str
.replace(/^[^A-Za-z0-9]*|[^A-Za-z0-9]*$/g, '')
.replace(/([a-z])([A-Z])/g, (m, a, b) => a + '_' + b.toLowerCase())
.replace(/[^A-Za-z0-9]+|_+/g, '_')
.toLowerCase();
}
toSnakeCase("hello world"); // "hello_world"