Javascript Split String for UpperCase Characters

How do you split a string into an array in Javascript by character UpperCase?

So I want to split:

'ThisIsTheStringToSplit' 

at

 ('This', 'Is', 'The', 'String', 'To', 'Split') 
+44
javascript
Oct 25 2018-11-11T00:
source share
3 answers

I would do it with .match() as follows:

 'ThisIsTheStringToSplit'.match(/[AZ][az]+/g); 

he will create such an array:

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

edit: since the string.split() method also supports regular expression, it can be achieved as follows

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

which will also solve the problem from the comment:

 "thisIsATrickyOne".split(/(?=[AZ])/); 
+79
Oct 25 '11 at 11:05
source share

Here you:)

 var arr = UpperCaseArray("ThisIsTheStringToSplit"); function UpperCaseArray(input) { var result = input.replace(/([AZ]+)/g, ",$1").replace(/^,/, ""); return result.split(","); } 
+4
Oct. 25 2018-11-11T00:
source share
 .match(/[AZ][az]+|[0-9]+/g).join(" ") 

This should also handle numbers. The join at the end brings all the elements of the array into a sentence, if that is what you are looking for

+2
Jul 15 '16 at 5:03
source share



All Articles