Insert a space before uppercase letters

I have the string "MySites" . I want to put a space between My and Sites .

How to do it in jQuery or javascript?

+83
javascript jquery string regex replace
Apr 7 2018-11-11T00:
source share
7 answers

You can simply add a space before each uppercase character and trim the leading and trailing spaces

 s = s.replace(/([AZ])/g, ' $1').trim() 
+127
Aug 22 '14 at 16:53
source share

This will allow you to find each occurrence of a lowercase character, followed by an uppercase character, and insert a space between them:

 s = s.replace(/([az])([AZ])/g, '$1 $2'); 

For special occasions when there are 2 uppercase letters in a row (for example, ThisIsATest), add an additional code below:

  s = s.replace(/([AZ])([AZ])/g, '$1 $2'); 
+121
Apr 07 2018-11-11T00:
source share

Can I suggest a slight change to the currently accepted answer:

 function insertSpaces(string) { string = string.replace(/([az])([AZ])/g, '$1 $2'); string = string.replace(/([AZ])([AZ][az])/g, '$1 $2') return string; } 

It means that:

 ACROText -> ACRO Text UserNameTest -> User Name Test 

Which might be a little more useful if you are dealing with db column names (and use some abbreviations for some things)

+18
Dec 16 '15 at 10:45
source share

This should insert a space between each capital letter, which would not be a capital letter.

 var myString = "MySites" var newString = ""; var wasUpper = false; for (var i = 0; i < myString.length; i++) { if (!wasUpper && myString[i] == myString.toUpperCase()[i]) { newString = newString + " "; wasUpper = true; } else { wasUpper = false; } newString = newString + myString[i]; } 

newString will have the desired value. Also, if you want to shorten your code with a regular expression, you can use the following code from camelCase Javascript for a regular form

 "thisStringIsGood" // insert a space before all caps .replace(/([AZ])/g, ' $1') // uppercase the first character .replace(/^./, function(str){ return str.toUpperCase(); }) 
+6
Apr 07 2018-11-11T00:
source share

regex to find lower case - upper case border and then insert space

 <div id='x'>ThisIsMySites</div> $('#x').text( $('#x').text().replace(/([az])([AZ])/g, "$1 $2") ); 

http://jsfiddle.net/uXy64/

+5
Apr 7 2018-11-14T00:
source share

You can use String#split() and a preview for a capital letter ( [AZ] ), and then Array#join() for an array with a space:

 let stringCamelCase = "MySites"; let string = stringCamelCase.split(/(?=[AZ])/).join(" "); console.log(string) 

Or, as a String Object function:

 String.prototype.cC2SC = function() { return this.split(/(?=[AZ])/).join(" "); } console.log("MyCamelCase".cC2SC()); 
+1
Aug 12 '18 at 23:27
source share

In one regular expression, replace (without trimming or joining), which allows any character, not just the letters [az] or [AZ] .

 const str = "MySites"; str.replace(/(?<!^)([AZ])/, " $1"); // -> "My Sites" 

You can check more about the look behind (?<!^) Here .

0
May 08 '19 at 3:58
source share



All Articles