JSFiddle - React, Tailwind, and code Playground

by jeffsturgis

JavaScript

var str = 'AStringOfChars';

// should output 'A String Of Chars'
function splitOnCaps(str, formattedStr) {
	var char = str.slice(0, 1);
  formattedStr = formattedStr || char;

  if (formattedStr !== char) {
  	formattedStr += char.match(/[A-Z]/) ? (' ' + char) : char;
  }
  
  if (str.length > 1) {
  	return splitOnCaps(str.substring(1), formattedStr);
  } else {
  	return formattedStr;
  }
};

console.log(splitOnCaps(str));