Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert dash-separated string to camelCase?

For example suppose I always have a string that is delimited by "-". Is there a way to transform

it-is-a-great-day-today

to

itIsAGreatDayToday

Using RegEx?

like image 926
Hoa Avatar asked May 03 '12 04:05

Hoa


People also ask

What is CamelCase example?

Meaning of camel case in English. the use of a capital letter to begin the second word in a compound name or phrase, when it is not separated from the first word by a space: Examples of camel case include "iPod" and "GaGa".

What is CamelCase conversion?

Camel case (sometimes stylized as camelCase or CamelCase, also known as camel caps or more formally as medial capitals) is the practice of writing phrases without spaces or punctuation. It indicates the separation of words with a single capitalized letter, and the first word starting with either case.


Video Answer


1 Answers

Yes (edited to support non-lowercase input and Unicode):

function camelCase(input) {      return input.toLowerCase().replace(/-(.)/g, function(match, group1) {         return group1.toUpperCase();     }); } 

See more about "replace callbacks" on MDN's "Specifying a function as a parameter" documentation.

The first argument to the callback function is the full match, and subsequent arguments are the parenthesized groups in the regex (in this case, the character after the the hyphen).

like image 188
apsillers Avatar answered Oct 09 '22 17:10

apsillers