1

I am splitting a string into words for any uppercase letter and I am using the following regex:

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

Javascript Split string on UpperCase Characters

However if there are two or more upper case letters in a row I only want to split from the first letter.

'THISIsTheStringToSplit' would split into the words THISIs, The, String, To, Split.

How can I use regex to handle this case?

anubhava
  • 713,503
  • 59
  • 514
  • 593
doorman
  • 13,315
  • 17
  • 64
  • 132

2 Answers2

4

Here you go:

var str = "THISIsTheStringToSplit".match(/[A-Z]+[a-z]*/g);

This will return an array with all the matches as required.

Also, this will handle the case where all alphabets are capital.

Example:

var str = "THISI".match(/[A-Z]+[a-z]*/g);

This will return THISI

3

Could you please try following, written as per shown samples.

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

It will simply match one or more occurrences of capital alphabets followed by 1 or more occurrences of small letters.

RavinderSingh13
  • 117,272
  • 11
  • 49
  • 86