admin管理员组

文章数量:1134246

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']

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']
Share Improve this question edited Jul 26, 2021 at 11:23 teckmk 4874 silver badges12 bronze badges asked Oct 25, 2011 at 10:58 Nicholas MurrayNicholas Murray 13.5k14 gold badges68 silver badges85 bronze badges 1
  • This could end up being useful for some people looking for a solution to this problem: stackoverflow.com/a/25732260/1454888 – Augusto Barreto Commented Mar 11, 2016 at 23:14
Add a comment  | 

9 Answers 9

Reset to default 352

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

// positive lookahead to keep the capital letters
r = 'ThisIsTheStringToSplit'.match(/[A-Z][a-z]+/g);

console.log(r)

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

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

console.log(r)

that will also solve the problem from the comment:

r = "thisIsATrickyOne".split(/(?=[A-Z])/);

console.log(r)

Update: to split on Uppercase character, not preceded with another Uppercase character, a negative lookbehind can be used

r = 'SplitOrderID'.split(/(?<![A-Z])(?=[A-Z])/);

console.log(r)

.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"

Here you are :)

var arr = UpperCaseArray("ThisIsTheStringToSplit");

function UpperCaseArray(input) {
    var result = input.replace(/([A-Z]+)/g, ",$1").replace(/^,/, "");
    return result.split(",");
}

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(' ');

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)

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']

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');

I've done something similar, but needed to support abbreviations (ie, ATMMachine).

I ended up using the pattern

let pattern = /([A-Z][a-z]+|[0-9]+|[A-Z]+?(?=[A-Z][a-z]|[0-9]|$))/g

Regex 101 Demo

The abbreviations threw a wrench into things because, in the case of an abbreviation followed by a word, I needed the match to split at the penultimate capital letter so that the trailing word had it's first letter (ie, ATMMachine became ATM Machine not ATMM achine). But if it was followed by the end of the string or a series of numbers, it should keep the last capital. This is something that required I use a lookahead.

Normally, I avoid using lookaheads for performance reasons, but I couldn't avoid it and still get the right behavior. I did make sure the lookahead was as simple as possible, and kept the match as short as possible (ie, no + quantifiers) to minimize the performance impact.

The lookahead ((?=) is in the third case in the regex, after a capitalized word or any number of digits, and is as follows:

[A-Z]+?(?=[A-Z][a-z]|[0-9]|$).

To match, first, the regex looks for a sequence of at least one capital letter, followed by either the start of a capitalized word ([A-Z][a-z]) (in which case the last capital in the sequence is given back to the lookahead), a sequence of numbers ([0-9]), or the end of the line ($).

you can then use like:

/**
 * attempts to humanize a camel cased name by applying the following rules:
 * - add a space before each capitalized word
 * - capitalize the first word
 * @param { string | null } name
 * @return { string } the humanized name
 */
function humanizeName(name) {
  if (!name || name.length < 1) {
    return '';
  }
  name = name[0].toUpperCase() + name.slice(1);
  return name.match(/([A-Z][a-z]+|[A-Z]+?(?=[A-Z][a-z]+|$))/g).join(' ');
}
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(',');

本文标签: Javascript Split string on UpperCase CharactersStack Overflow