
编写一个函数,将单个camelcase格式的字符串转换为snake_case格式的字符串。
网页中拖动 DIV 是很常见的操作,今天就分享给大家一个 jQuery 多列网格拖动布局插件,和其它的插件不太一样的地方在于你处理拖放的元素支持不同大小,并且支持多列的网格布局,它们会自动的根据位置自己排序和调整。非常适合你开发具有创意的应用。这个插件可以帮助你将任何的 HTML 元素转换为网格组件
解决方案
// 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









