158

How do you split a string into an array in JavaScript by Uppercase character?

So I wish to split:

'ThisIsTheStringToSplit'

into

['This', 'Is', 'The', 'String', 'To', 'Split']
skmak
  • 447
  • 4
  • 12
Nicholas Murray
  • 13,305
  • 14
  • 65
  • 84
  • This could end up being useful for some people looking for a solution to this problem: http://stackoverflow.com/a/25732260/1454888 – Augusto Barreto Mar 11 '16 at 23:14

8 Answers8

337

I would do this with .match() like this:

'ThisIsTheStringToSplit'.match(/[A-Z][a-z]+/g);

it will make an array like this:

['This', 'Is', 'The', 'String', 'To', 'Split']

edit: since the string.split() method also supports regex it can be achieved like this

'ThisIsTheStringToSplit'.split(/(?=[A-Z])/); // positive lookahead to keep the capital letters

that will also solve the problem from the comment:

"thisIsATrickyOne".split(/(?=[A-Z])/);
Teneff
  • 30,564
  • 13
  • 72
  • 103
  • 64
    This will not find single uppercase characters. I suggest the following: `"thisIsATrickyOne".match(/([A-Z]?[^A-Z]*)/g).slice(0,-1)` – andrewmu Oct 25 '11 at 11:25
  • 4
    Back into a readable string `"thisIsATrickyOne".match(/([A-Z]?[^A-Z]*)/g).slice(0,-1).join(" ")` gives `this Is A Tricky One` – Simon Hutchison Jun 02 '21 at 00:14
40
.match(/[A-Z][a-z]+|[0-9]+/g).join(" ")

This should handle the numbers as well.. the join at the end results in concatenating all the array items to a sentence if that's what you looking for

'ThisIsTheStringToSplit'.match(/[A-Z][a-z]+|[0-9]+/g).join(" ")

Output

"This Is The String To Split"
Max
  • 589
  • 5
  • 6
  • 2
    This is perfect. But anyone using this should be careful in the following case: `'ThisIs8TheSt3ringToSplit'.match(/[A-Z][a-z]+|[0-9]+/g).join(" ")` will output `This Is 8 The St 3 To Split`, ommitting the small case string(`ring`) after `3`. – Diablo Jul 19 '19 at 09:32
13

Here you are :)

var arr = UpperCaseArray("ThisIsTheStringToSplit");

function UpperCaseArray(input) {
    var result = input.replace(/([A-Z]+)/g, ",$1").replace(/^,/, "");
    return result.split(",");
}
Manuel van Rijn
  • 10,170
  • 1
  • 29
  • 52
9

This is my solution which is fast, cross-platform, not encoding dependent, and can be written in any language easily without dependencies.

var s1 = "ThisЭтотΨόυτÜimunəՕրինակPříkladדוגמאΠαράδειγμαÉlda";
s2 = s1.toLowerCase();
result="";
for(i=0; i<s1.length; i++)
{
 if(s1[i]!==s2[i]) result = result +' ' +s1[i];
 else result = result + s2[i];
}
result.split(' ');
5

Here's an answer that handles numbers, fully lowercase parts, and multiple uppercase letters after eachother as well:

const wordRegex = /[A-Z]?[a-z]+|[0-9]+|[A-Z]+(?![a-z])/g;
const string = 'thisIsTHEString1234toSplit';
const result = string.match(wordRegex);

console.log(result)
Marcus
  • 321
  • 4
  • 10
  • 1
    This is the most comprehensive answer as far as I can tell. The other ones won't handle "aStringABC123" whereas this one will return "a String ABC 123". – dgurns Mar 08 '23 at 14:09
1

This piece of code will return the following output

const str = 'ThisIsTheStringToSplit';
str.split(/(?=[A-Z])/);

// It will print this output        
['This', 'Is', 'The', 'String', 'To', 'Split']
Baqer Naqvi
  • 6,011
  • 3
  • 50
  • 68
0

I'm a newbie in programming and this was my way to solve it, using just basics of JavaScript declaring variables as clean for someone reading as possible, please don't kill me if it is not optimized at all, just starting with coding hehe :)

  function solution(string) {

     let newStr = '';

     for( i = 0; i < string.length ; i++ ) {
       const strOriginal = string[i];
       const strUpperCase = string[i].toUpperCase();

       if( strOriginal === strUpperCase) {
         newStr = newStr + ' ' + strOriginal;
       } else {
         newStr = newStr + strOriginal;
       }

     }

     return console.log(newStr);

   }


   solution('camelCasing');
-1
string DemoStirng = "ThisIsTheStringToSplit";
            for (int i = 0; i < DemoStirng.Length; i++) {
                if (i != 0)
                {
                    if ((int)DemoStirng[i] <= 90 && (int)DemoStirng[i] >= 65)
                    {
                        var aStringBuilder = new StringBuilder(DemoStirng);
                        aStringBuilder.Insert(i, ",");
                        DemoStirng = aStringBuilder.ToString();
                        i++;
                    }
                }
            }
            string[] words = DemoStirng.Split(',');
Askar
  • 1
  • 2