はてなダイアリー


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