本文介绍了如何在Javascript中将驼峰式转换为蛇形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用TypeScript将驼峰式的字符串转换为蛇形.
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.
示例:fieldName
到field_name
应该是有效的转换,但是FieldName
到Field_Name
无效.
Example: fieldName
to field_name
should be a valid conversion, but FieldName
to Field_Name
is not valid.
推荐答案
const camelToSnakeCase = str => str.replace(/[A-Z]/g, letter => `_${letter.toLowerCase()}`);
这篇关于如何在Javascript中将驼峰式转换为蛇形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!