Split a string on a capital letter or numbers

I guess it depends on the string's conventions which may increase the complexity

// here 'TIMES' & 'with' are seperated (example 2)
const str = 'SplittingStringsIsFunTimesA100000aaaTIMESwithFollowUp';

// here 'TIMES' & 'With' are seperated (exmpaple 3)
const str2 = 'SplittingStringsIsFunTimesA100000aaaTIMESWithCAPITAL5FollowUp';


// 1. USING REGEX - MATCH
console.log(
  '1. USING REGEX:\n',
  str
  .match(/(\d+|[a-z]+|[A-Z][a-z]*)/g)
  .join(' ')
);


// 2. USING REGEX - MATCH (KEEP ALL CAPITAL CHARS)
console.log(
  '2. USING REGEX (GROUP ALL):\n',
  str
  .match(/(\d+|[a-z]+|([A-Z]([A-Z]+|[a-z]*)))/g)
  .join(' ')
);

// 3. USING REGEX - MATCH (KEEP CAPITAL CHARS BUT LAST)
console.log(
  '3. USING REGEX (GROUP BUT LAST):\n',
  str2
  .match(/(\d+|[a-z]+|([A-Z]([a-z]+|([A-Z]+(?![a-z]))?)))/g)
  .join(' ')
);


// 4. USING SPLIT - FILTER
console.log(
  '4. USING SPLIT:\n',
  str2
  .split(/(\d+|[A-Z][a-z]*)/)
  .filter(v => v !== '')
  .join(' ')
);

Just replace any uppercase letter [A-Z] or any sequence of digits \d+ with a space plus what we just matched " $1". We skip the first letter so that no space will be added at the begining of the resulting string by adding a negative lookahead on the start of the string (?!^):

// ...

return value.replace(/(?!^)([A-Z]|\d+)/g, " $1");

Example:

let value = "ANet15Amount";

let result = value.replace(/(?!^)([A-Z]|\d+)/g, " $1");

console.log(result);

How about matching by a more basic pattern like this and joining with space.

let str = `ANet15Amount`;

let camel = str.match(/[A-Z]+(?![a-z])|[A-Z]?[a-z]+|\d+/g).join(' ');

console.log(camel);

First I thought of simply [A-Z][a-z]*|\d+ but this would break eg ABCDefg123 into A B C Defg 123 which would be different working to your current function, that transforms to ABC Defg 123.

There is still a little difference. Yours transforms A1B2 to A 1B 2 and this one to A 1 B 2 where I think this one would be more accurate, wouldn't it.


Try [A-Z]?[a-z]+|[A-Z]|[0-9]+

  • 0 or 1 uppercase letter directly followed by 1 or more lowercase letters
  • or 1 uppercase letter
  • or 1 or more digits

Test in generator: https://regex101.com/r/uBO0P5/1