Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Space between lowercase and uppercase letters in a string in JavaScript

I want to add a space between a lowercase and uppercase in one string. For example:

FruityLoops
FirstRepeat

Now I want to add a space between the lowercase and uppercase letters. I don't know how I should start in JavaScript. Something with substr or search? Can somebody can help me?

like image 995
Frank Avatar asked Jan 27 '11 17:01

Frank


People also ask

How do you put a space before a capital letter in a string?

Use the replace() method to insert a space before the capital letters in a string, e.g. str. replace(/[A-Z]/g, ' $&'). trim() . The replace method will return a new string, where each capital letter is replaced by a space preceding the capital letter.

How do I add a space between characters in a string?

To add a space between the characters of a string, call the split() method on the string to get an array of characters, and call the join() method on the array to join the substrings with a space separator, e.g. str. split('').

How do you use lowercase and uppercase in JavaScript?

JavaScript provides two helpful functions for converting text to uppercase and lowercase. String. toLowerCase() converts a string to lowercase, and String. toUpperCase() converts a string to uppercase.


1 Answers

var str = "FruityLoops";

str = str.replace(/([a-z])([A-Z])/g, '$1 $2');

Example: http://jsfiddle.net/3LYA8/

like image 107
user113716 Avatar answered Sep 27 '22 15:09

user113716