Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert camelcase to snake case in Javascript? [closed]

I want to convert a string of that is in camel case to snake case using TypeScript.

Remember that the "snake case" refers to the format style in which each space is replaced by an underscore (_) character and the first letter of each word written in lowercase.

Example: fieldName to field_name should be a valid conversion, but FieldName to Field_Name is not valid.

like image 671
Badrul Avatar asked Jan 18 '19 01:01

Badrul


People also ask

Is JavaScript snake case or camelCase?

camelCase is used by JavaScript itself, by jQuery, and other JavaScript libraries.

Is snake case or camelCase better?

Screaming snake case is used for variables. Scripting languages, as demonstrated in the Python style guide, recommend snake case in the instances where C-based languages use camel case.

What is camelCase in JavaScript?

Camel case is the practice of writing phrases such that each word or abbreviation in the middle of the phrase begins with a capital letter, with no intervening spaces or punctuation. For example, Concurrent hash maps in camel case would be written as − ConcurrentHashMaps.


1 Answers

const camelToSnakeCase = str => str.replace(/[A-Z]/g, letter => `_${letter.toLowerCase()}`); 
like image 190
Nobody Avatar answered Oct 04 '22 11:10

Nobody