2007-05-11

Camel Case to Underscores and Back

To convert camelCase to underscores, in TextMate, replace ([a-z])([A-Z]) with $1_\l$2

To convert underscores to camelCase, replace ([a-z]+)_([a-z]+) with $1\u$2

2 comments:

Brenton said...

Here's a tab trigger I wrote for adding event listeners:

addEventListener(${1:MouseEvent}.${2:CLICK}, ${3:on${2/(_)?([A-Z])([A-Z]+)/\u$2\L$3/g}}, ${4:false}, ${5:0}, ${6:true});

It assumes the event name is a constant (UPPER_CASED). The interesting part is here:

${2/(_)?([A-Z])([A-Z]+)/\u$2\L$3/g}}

It takes EVENT_TYPE from $2 and mirrors it as EventType.

brwalias said...

where can i implment the camelCase globally?