7

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?

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
Frank
  • 499
  • 2
  • 10
  • 22

4 Answers4

22
var str = "FruityLoops";

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

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

user113716
  • 318,772
  • 63
  • 451
  • 440
  • @Matt: It's better to have been up-voted and lost, than to have never been up-voted at all. :o) – user113716 Jan 27 '11 at 17:49
  • 3
    @Frank, @patrick: Note that this will not add spaces between adjacent capitals, e.g. "FruityXLoops" => "Fruity XLoops", not "Fruity X Loops". Whether that's a feature or a bug is down to you, Frank. :-) – T.J. Crowder Jan 27 '11 at 17:59
  • @patrick: hey now, I re-upvoted when you corrected the bug in your answer. – Matt Ball Jan 27 '11 at 18:03
  • @Matt: Yes I saw that you re-up-voted. It was a poor transfer between a console version and a jsFiddle version. My console version worked, but was looser, then somewhere in the mix I only grabbed part of the revision. I figured that's why you removed it at first, and appropriate it was. Yoda? – user113716 Jan 27 '11 at 19:59
  • @T.J. Crowder: Feature. *Always* a feature. ;o) Actually OP does state *"a space between the lowercase and uppercase"*. – user113716 Jan 27 '11 at 20:02
3

something simple like that :

"LoL".replace(/([a-z])([A-Z])/g, "$1 $2")

is maybe sufficient ;)

Arnaud F.
  • 8,252
  • 11
  • 53
  • 102
2

You can do it with a manual search, but it may be easier with a regex. Assuming:

  • You know it starts with a capital
  • You don't want a space in front of that capital
  • You want a space in front of all subsequent capitals

Then:

function spacey(str) {  
    return str.substring(0, 1) +
           str.substring(1).replace(/[A-Z]/g, function(ch) {
        return " " + ch;
    });
}

alert(spacey("FruitLoops")); // "Fruit Loops"

Live example

More efficient version inspired by (but different from) patrick's answer:

function spacey(str) {  
    return str.substring(0, 1) +
           str.substring(1).replace(/([a-z])?([A-Z])/g, "$1 $2");
}

alert(spacey("FruityLoops"));  // "Fruity Loops"
alert(spacey("FruityXLoops")); // "Fruity X Loops"

Live example

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Only solution I could find that deals with adjacent capital letters!! +1, good sir. – Aaron Jan 09 '15 at 11:45
  • What does this mean - /([a-z])?([A-Z])/g, "$1 $2" ??? Look for small words that have small case letter followed by big. The small is optional. Put a space between the first group (small letter) and second group (capital)? – stack1 Aug 11 '15 at 00:06
  • @stack1: Not words, characters. Other than that, yes, spot-on. – T.J. Crowder Aug 11 '15 at 05:25
0

The regexp option looks the best. Getting the regexp right appears to be tricky though.

There's another question here with some more complex options to try:

Regular expression, split string by capital letter but ignore TLA

Community
  • 1
  • 1
Jon Egerton
  • 40,401
  • 11
  • 97
  • 129