# Insert a space in front of capital letter in a string

**URL:** <https://forum.kirupa.com/t/insert-a-space-in-front-of-capital-letter-in-a-string/302427>\
**Category:** flash\
**Created:** [December 30, 2009, 11:24am UTC](https://forum.kirupa.com/t/insert-a-space-in-front-of-capital-letter-in-a-string/302427 "2009-12-30T11:24:31Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![rockfish](https://avatars.discourse-cdn.com/v4/letter/r/f9ae1b/32.png) [@rockfish](https://forum.kirupa.com/u/rockfish)\
**Post date:** [December 30, 2009, 11:24am UTC](https://forum.kirupa.com/t/insert-a-space-in-front-of-capital-letter-in-a-string/302427/1 "2009-12-30T11:24:31Z")

</div>

Hi I’m hoping someone can help

I neeed to insert spaces in front of capital letters in a string. the string contains the name of a country instance for example demRepublicOfCongo. I would like to scan the string and inset spaces before each capital letter so that the contents of the above string would become dem Republic Of Congo, for eaxmple.

I found some code that I thought would help but this doesn’t seem to work. It is posted below. I would be grateful if anyone could tell me where I a going wrong.

thanks

//Works out country name from string  
var country:String=counrtyinstance.replace("\_mc","");  
var re:RegExp = /(?\<=[a-z])([A-Z])/g;  
var re2:RegExp = /(?\<=\w)([A-Z])/g;  
country.replace(re," $1")  
country.replace(re2," $1")  
trace (country);
