Вот решение для регулярного выражения:
Первая строчная строка:
str = str.toLowerCase();
Замените все _
и пробелы и первые символы в слове заглавными буквами:
str = str.replace(/(?:_| |\b)(\w)/g, function(str, p1) { return p1.toUpperCase()})
DEMO
Обновление: Меньше шагов;)
Объяснение:
/ // start of regex
(?: // starts a non capturing group
_| |\b // match underscore, space, or any other word boundary character
// (which in the end is only the beginning of the string ^)
) // end of group
( // start capturing group
\w // match word character
) // end of group
/g // and of regex and search the whole string
Значение группы захвата доступно в функции как p1
, а выражение целом заменяется возвращаемым значением функции.