Javascript regex - camel to file

Anyone has a regex in javascript to convert:

someCamelCase to file file

or

SomeCamelCase to file file

??

If so, it will be very helpful.

Thank.

+5
source share
3 answers

You can make a simple regular expression to write a lowercase letter adjacent to uppercase, insert a stroke between them and make the result lowercase.

For instance:

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

fileCase('SomeCamelCase'); // "some-camel-case"
fileCase('someCamelCase'); // "some-camel-case"
+6
source

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();});
}
+1
source

Camel Case <= > :

: String , .

camelCase , .

String.prototype.camelToHyphen = function() {
  return this.replace(/((?!^)[A-Z])/g, '-$1').toLowerCase();
};

.

String.prototype.hyphenToCamel = function() {
  return (/-[a-z]/g.test(this)) ? this.match(/-[a-z]/g).map(function(m, n){
    return m.replace(n, n.toUpperCase()[1]);
  }, this) : this.slice(0);
};

I believe these are common problems, but I could not find anything that summarized them that way.

0
source

All Articles