Forum Moderators: open

Message Too Old, No Replies

Split CamelCase string into words

         

trevordixon

4:10 pm on Apr 28, 2008 (gmt 0)

10+ Year Member



How do I split a camelcased string into words? split(/[A-Z]/) almost works, but it drops the first letter of each word. How can I modify that regular expression so I get all of the characters?

trevordixon

4:19 pm on Apr 28, 2008 (gmt 0)

10+ Year Member



Hey hey, if I use /([A-Z])/ instead of /[A-Z]/ I get the delimiters too, but in a different array element. Anybody know how I can keep the delimiter with the rest of the word?

Example:
str = "camelCasedWord";
document.write(str.split(/[A-Z]/)); //writes camel,ased,ord

str = "camelCasedWord";
document.write(str.split(/([A-Z])/)); //writes camel,C,ased,W,ord

I want it to return camel,Cased,Word

Dabrowski

4:44 pm on Apr 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think you may need 2 regex's, I'm thinking in Perl here but it's mostly the same.

First make actual separate words:

replace( /([a-z])([A-Z])/, "$1 $2");

Then split on the space:

split( /\s/);

httpwebwitch

5:17 pm on Apr 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



hey, you could forgo the regex and do this with a loop. I know regex purists may not like that idea... but if it works, do it, then refactor it with a regex solution if you figure one out later.

var words = [];
var j=0;
for (var i=0;i<str.length;i++){
var thisletter = str.substring(i,i);
if(thisletter == thisletter.toUpperCase()){ // it's an upper case letter
j++;
}
words[j] = words[j] + thisletter;
}

this is a simple, untested snippet... side effect would be if the word starts with a capital letter, array[0] would be blank and the first word would begin at array[1]

trevordixon

6:40 pm on Apr 28, 2008 (gmt 0)

10+ Year Member



Thank you both for your help. I think I'll go with Dabrowski's suggestion for now. Thanks!

coopster

7:38 pm on Apr 28, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



var str = "camelCasedWord" 
alert(str.replace(/([A-Z])/g, ",$1"))

Dabrowski

9:26 pm on Apr 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Whoa httpwebwitch!

Why do all that when 2 lines of Regexp is enough?