I have a string \"MySites\"
. I want to place a space between My
and Sites
.
How can I do this in jQuery or JavaScript?
You can use String#split()
and a look-ahead for the capitalized alphabet ([A-Z]
) and then Array#join()
the array with a space:
let stringCamelCase = "MySites";
let string = stringCamelCase.split(/(?=[A-Z])/).join(" ");
console.log(string)
Or, as a String Object function:
String.prototype.cC2SC = function() {
return this.split(/(?=[A-Z])/).join(" ");
}
console.log("MyCamelCase".cC2SC());