Write a function that takes a single string in camelCase format and converts it into a string in snake_case format.
// 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的詳細內容。更多資訊請關注PHP中文網其他相關文章!