在JavaScript中将camelToCase转换为snake_case
来源:dev.to
时间:2024-10-10 10:39:44 451浏览 收藏
在IT行业这个发展更新速度很快的行业,只有不停止的学习,才不会被行业所淘汰。如果你是文章学习者,那么本文《在JavaScript中将camelToCase转换为snake_case》就很适合你!本篇内容主要包括##content_title##,希望对大家的知识积累有所帮助,助力实战开发!

编写一个函数,将单个camelcase格式的字符串转换为snake_case格式的字符串。
解决方案
// a function cameltocase that takes a string (text) as the parameter.
function cameltocase(text) {
// transform each uppercase letter (character) based on its position.
function uppertounderscorelower(character, position) {
// if the letter isn't the first character, add an underscore and convert it to lowercase.
return (position > 0 && "_") + match.tolowercase();
}
// replace all uppercase letters in (text) by calling the uppertounderscorelower function.
return text.replace(/[a-z]/g, uppertounderscorelower);
}
console.log(cameltocase("cameltocase"));
结果
> camel_to_case
到这里,我们也就讲完了《在JavaScript中将camelToCase转换为snake_case》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!
声明:本文转载于:dev.to 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
最新阅读
更多>
-
214 收藏
-
275 收藏
-
143 收藏
-
375 收藏
-
168 收藏
-
363 收藏
-
129 收藏