Reputation: 1189
In javascript json column names, I want to Split camel case string for example CustomerID => Customer ID
Below code working but splitting as Customer I D
var title = field.replace(/([a-zA-Z][a-z]*)/g, " $1");
I don't want to split abbreviated words or consecutive Caps, how can I achieve this.
Also I want to make first letter uppercase if it is a small letter.
Upvotes: 1
Views: 460
Reputation: 207521
You can pass a function into the replace() and call toUpperCase
"custID".replace(/([a-z])([A-Z])/g,"$1 $2").replace(/^([a-z])/, function(a){ return a.toUpperCase();});
Upvotes: 3
Reputation: 66663
You can use:
var title = field
.replace(/([a-z]+)([A-Z]+)/g, "$1 $2") // "aaAA" => "aa AA"
.replace(/([A-Z]+)([a-z]+)/g, "$1 $2") // "AAaa" => "AA aa"
.replace(/^([a-z])/g, function(x, y) { return y.toUpperCase(); }); // caps first char
If field
is "aaaAAAbbDDD"
, the above will return "Aaa AAA bb DDD"
Upvotes: 0
Reputation: 43673
= field.charAt(0).toUpperCase() + field.replace(/([a-z])(?=[A-Z])/g, "$1 ").slice(1);
Upvotes: 1