Space between lowercase and uppercase letters in a string in JavaScript

Frank picture Frank · Jan 27, 2011 · Viewed 8.4k times · Source

I want to add a space between a lowercase and uppercase in one string. For example:

FruityLoops
FirstRepeat

Now I want to add a space between the lowercase and uppercase letters. I don't know how I should start in JavaScript. Something with substr or search? Can somebody can help me?

Answer

user113716 picture user113716 · Jan 27, 2011
var str = "FruityLoops";

str = str.replace(/([a-z])([A-Z])/g, '$1 $2');

Example: http://jsfiddle.net/3LYA8/