Javascript/jQuery: Split camelcase string and add hyphen rather than space
Solution 1:
Try something like:
var myStr = 'thisString';
myStr = myStr.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
Solution 2:
Late to answer, but this solution will work for cases where a single letter is camel cased.
'thisIsATest'.replace(/([a-zA-Z])(?=[A-Z])/g, '$1-').toLowerCase(); // this-is-a-test
Solution 3:
Try the following:
var token = document.getElementsByTagName('strong')[0].innerHTML,
replaced = token.replace(/[a-z][A-Z]/g, function(str, offset) {
return str[0] + '-' + str[1].toLowerCase();
});
alert(replaced);
Example - http://jsfiddle.net/7DV6A/2/
Documentation for the string replace
function:
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/replace
Solution 4:
String.prototype.camelCaseToDashed = function(){
return this.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
}
// Usage
"SomeVariable".camelCaseToDashed();