Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Regex - Camel to File Case

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.

like image 955
Scott Klarenbach Avatar asked Sep 09 '10 01:09

Scott Klarenbach


2 Answers

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"
like image 142
Christian C. Salvadó Avatar answered Oct 18 '22 21:10

Christian C. Salvadó


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();});
}
like image 42
gianebao Avatar answered Oct 18 '22 20:10

gianebao