views:

36

answers:

2

Anyone have a regex in javascript for converting:

someCamelCase into some-file-case

or

SomeCamelCase into some-file-case

??

If so, that would be very helpful.

Thanks.

+1  A: 

Here. try this one.

"SomeCamelCase".replace(/[A-Z]/g, function(m){return '_' + m.toLowerCase();});

or as a function

function camelToHiphen(str){
    return str.replace(/[A-Z]/g, function(m){return '_' + m.toLowerCase();});
}
sheeks06
This returns "_some_camel_case". It might be a good idea to trim the first underscore from the string. Otherwise +1 it works.
Keyo
Sorry 'bout that. haven't tested it. just built the code in my head. :)
sheeks06
+4  A: 

You can make a simple regexp to capture a lowercase letter contiguous to an uppercase one, insert a dash between both and make the result all lowercase.

For example:

function fileCase(str) {
  return str.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase();
}

fileCase('SomeCamelCase'); // "some-camel-case"
fileCase('someCamelCase'); // "some-camel-case"
CMS