はてなダイアリー
806 ワード
Regexは本当に見つけると/または物事を置き換えるために最適です.
私は
そして、キャメルケース
私は
my-file-name.js
またはmy_file_name.js
というファイルを持っているそして、キャメルケース
myFileName.js
にそれらを変更したい.function toCamelCase(name) {
// search for "-" or "_" followed by a character and replace it uppercased
return name.replace(/[-_]([a-z])/gi, (_, char) => char.toUpperCase());
}
解説/ => starts the regex
[-_] => - or _
([a-z]) => all lowercase letters, captured in a group
/ => ends the regex
gi => search all occurencies, ignore case
(_, char) => char.toUpperCase() => _ is the passed in complete match, don't need it;
return the capture group (= the letter after - or _) uppercased
Reference
この問題について(はてなダイアリー), 我々は、より多くの情報をここで見つけました https://dev.to/miku86/diary---20180905-15fiテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol