camelcasing

Convert kebab-case to camelCase - Javascript

安稳与你 提交于 2020-08-19 12:17:51
问题 Say I have a function that transforms kebab-case to camelCase : camelize("my-kebab-string") == 'myKebabString'; I'm almost there, but my code outputs the first letter with uppercase too: function camelize(str){ let arr = str.split('-'); let capital = arr.map(item=> item.charAt(0).toUpperCase() + item.slice(1).toLowerCase()); let capitalString = capital.join(""); console.log(capitalString); } camelize("my-kebab-string"); 回答1: To keep your existing code, I've just added a check on the index

Convert kebab-case to camelCase - Javascript

守給你的承諾、 提交于 2020-08-19 12:15:10
问题 Say I have a function that transforms kebab-case to camelCase : camelize("my-kebab-string") == 'myKebabString'; I'm almost there, but my code outputs the first letter with uppercase too: function camelize(str){ let arr = str.split('-'); let capital = arr.map(item=> item.charAt(0).toUpperCase() + item.slice(1).toLowerCase()); let capitalString = capital.join(""); console.log(capitalString); } camelize("my-kebab-string"); 回答1: To keep your existing code, I've just added a check on the index

How to convert a camel-case string to dashes in JavaScript?

╄→尐↘猪︶ㄣ 提交于 2020-05-26 10:42:07
问题 I want to convert these strings: fooBar FooBar into: foo-bar -foo-bar How would I do this in JavaScript the most elegant and performant way for any given string? 回答1: You can use replace with a regex like: let dashed = camel.replace(/[A-Z]/g, m => "-" + m.toLowerCase()); which matches all uppercased letters and replace them with their lowercased versions preceded by "-" . Example: console.log("fooBar".replace(/[A-Z]/g, m => "-" + m.toLowerCase())); console.log("FooBar".replace(/[A-Z]/g, m =>

Regular rxpression to convert a camel case string into kebab case

本秂侑毒 提交于 2020-05-15 06:49:30
问题 function hyphenate(str) { var replace = "-"; str = str.toLowerCase().replace(/[\s_\b]/g, replace); console.log(str); return str; } hyphenate("This Is Hyphenate"); // this-is-hyphenate hyphenate("camelCaseString"); // camel-case-string I am trying to get my code to produce the results of the second function call, but have not identified a pattern that can do this. Any assistance would be greatly appreciated. 回答1: Note that \b in your [\s_\b] means a backspace character. Not sure you really

Regular rxpression to convert a camel case string into kebab case

柔情痞子 提交于 2020-05-15 06:48:40
问题 function hyphenate(str) { var replace = "-"; str = str.toLowerCase().replace(/[\s_\b]/g, replace); console.log(str); return str; } hyphenate("This Is Hyphenate"); // this-is-hyphenate hyphenate("camelCaseString"); // camel-case-string I am trying to get my code to produce the results of the second function call, but have not identified a pattern that can do this. Any assistance would be greatly appreciated. 回答1: Note that \b in your [\s_\b] means a backspace character. Not sure you really