469,271 Members | 1,001 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

split camelcase string into array of words words

Hi,

I'm looking for an optimal javascript function to split a camelcase
string and return an array.

I suppose one could loop through the string, check if character is
uppercase and start building a new word to add to the array but that
seems incredibly wasteful. must be some easy way to do it.
Thanks.

Jul 21 '06 #1
10 11337

"pantagruel" <ra*************@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
Hi,

I'm looking for an optimal javascript function to split a camelcase
string and return an array.

I suppose one could loop through the string, check if character is
uppercase and start building a new word to add to the array but that
seems incredibly wasteful. must be some easy way to do it.
This was a fun little morning brain warmer. ;^)

I've created a function that, I think, will do what you want. It's not that
pretty but it seem to work. In my example I've added it to String prototype
(making it available to all strings) but you can put it anyplace you like...
you could also condense it down to one line of code and append it to any
string but don't do that: functions should be used to abstract ugly code
like this. ;^)

Here it is:

String.prototype.CamelCaseToArray = function() {
// Preceed Uppercase (or sets of) with commas then remove any
leading comma
var Delimed = this.replace(/([A-Z]+)/g, ",$1").replace(/^,/, "");
// Split the string on commas and return the array
return Delimed.split(",");
};

Basically this does a regular expression search for uppercase letters (or
sets of uppercase letters) and preceeds them with commas. Since the string
may now have a comma as it's first character another replace is done to
remove it (if it exists). Then the whole mess is split on the commas and
returned.

Here's an example of how you might use it:

TestString = "MyCamelCASEString";

alert(TestString.CamelCaseToArray());

This function will (I hope obviously) have problems if there are commas in
the string to begin with. I've tested it on a handful of strings but you
should vette it more before trusting it.

I think (since I'm only caring to look for uppercase characters) that it
should be blissfully unfazed by numbers and punctuation (other than the
comma).

Hope this helps,

Jim Davis
Jul 21 '06 #2
pantagruel wrote:
Hi,

I'm looking for an optimal javascript function to split a camelcase
string and return an array.

I suppose one could loop through the string, check if character is
uppercase and start building a new word to add to the array but that
seems incredibly wasteful. must be some easy way to do it.
Thanks.
"CamelCaseString".match(/[A-Z][a-z]*/g)

returns array:

("Camel", "Case", "String").

Seems like what you want :-)

Jeremy
Jul 21 '06 #3
"Jeremy" <je****@pinacol.comwrote in message
news:%F7wg.14632$Nv.7608@fed1read10...
pantagruel wrote:
>Hi,

I'm looking for an optimal javascript function to split a camelcase
string and return an array.

I suppose one could loop through the string, check if character is
uppercase and start building a new word to add to the array but that
seems incredibly wasteful. must be some easy way to do it.

"CamelCaseString".match(/[A-Z][a-z]*/g)

returns array:

("Camel", "Case", "String").

Seems like what you want :-)
But this:

"camelCaseString".match(/[A-Z][a-z]*/g)

Returns: ["Case", "String"].

And this:

"camelCaseSTRING".match(/[A-Z][a-z]*/g)

Returns: ["Case", "S", "T", "R", "I", "N", "G"]

So... well... neener neener neener. ;^)

(Yeah... I'm a big jackass.)

Jim Davis
Jul 21 '06 #4
>
Here's an example of how you might use it:

TestString = "MyCamelCASEString";

alert(TestString.CamelCaseToArray());

Haven't tested yet but on appearance looks like it will work with the
strings I have to deal with, upper and lower camel cased strings that
need to be split into words. Since there is no punctuation allowed in
the strings I can't see this as being a problem.

Thanks for the help, will credit you in the comments of the script.

Cheers,
Bryan Rasmussen

Jul 21 '06 #5
Jim Davis wrote:
>
But this:

"camelCaseString".match(/[A-Z][a-z]*/g)

Returns: ["Case", "String"].

And this:

"camelCaseSTRING".match(/[A-Z][a-z]*/g)

Returns: ["Case", "S", "T", "R", "I", "N", "G"]

So... well... neener neener neener. ;^)

(Yeah... I'm a big jackass.)

Jim Davis

Those aren't camel case strings, now are they? :-) You can't expect any
algorithm that targets camel case strings to work on non-camel-case strings.

Jeremy

Jul 21 '06 #6
Jeremy wrote:
Jim Davis wrote:
>>
But this:

"camelCaseString".match(/[A-Z][a-z]*/g)

Returns: ["Case", "String"].

And this:

"camelCaseSTRING".match(/[A-Z][a-z]*/g)

Returns: ["Case", "S", "T", "R", "I", "N", "G"]

So... well... neener neener neener. ;^)

(Yeah... I'm a big jackass.)

Jim Davis

Those aren't camel case strings, now are they? :-) You can't expect any
algorithm that targets camel case strings to work on non-camel-case
strings.

Jeremy
Besides, by tweaking the regex a little you can account for
lower-camel-case:

"lowerCamelCase".match(/[A-Z]?[a-z]+/g)

returns {"lower", "Camel", "Case"}

Anything else (i.e. "camelCaseSTRING") is not a camel case string. I
would argue that in this case STRING must be an acronym and there's no
reason it should come back in all one string in the result array.

So, strictly speaking... neener neener :-)

Jeremy
Jul 21 '06 #7
JRS: In article <ks******************************@giganews.com>, dated
Fri, 21 Jul 2006 13:35:43 remote, seen in news:comp.lang.javascript, Jim
Davis <ne********@vboston.composted :
>"Jeremy" <je****@pinacol.comwrote in message
news:%F7wg.14632$Nv.7608@fed1read10...
>pantagruel wrote:
>>I'm looking for an optimal javascript function to split a camelcase
string and return an array.
>"CamelCaseString".match(/[A-Z][a-z]*/g)

returns array:

("Camel", "Case", "String").

Seems like what you want :-)

But this:

"camelCaseString".match(/[A-Z][a-z]*/g)

Returns: ["Case", "String"].

But this : "camelCaseString".match(/(\b|[A-Z])[a-z]*/g)
returns ['camel','Case','String']

And this : "camelCaseSTRing".match(/(\b|[A-Z]+)[a-z]*/g)
returns ['camel','Case','STRing']

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 21 '06 #8
Dr John Stockton wrote:
>
But this : "camelCaseString".match(/(\b|[A-Z])[a-z]*/g)
returns ['camel','Case','String']

And this : "camelCaseSTRing".match(/(\b|[A-Z]+)[a-z]*/g)
returns ['camel','Case','STRing']
What does \b do? Never seen that before.

Jeremy
Jul 21 '06 #9
"Dr John Stockton" <jr*@merlyn.demon.co.ukwrote in message
news:Ar**************@merlyn.demon.co.uk...
JRS: In article <ks******************************@giganews.com>, dated
Fri, 21 Jul 2006 13:35:43 remote, seen in news:comp.lang.javascript, Jim
Davis <ne********@vboston.composted :
>>"Jeremy" <je****@pinacol.comwrote in message
news:%F7wg.14632$Nv.7608@fed1read10...
>>pantagruel wrote:
>snip<
But this : "camelCaseString".match(/(\b|[A-Z])[a-z]*/g)
returns ['camel','Case','String']

And this : "camelCaseSTRing".match(/(\b|[A-Z]+)[a-z]*/g)
returns ['camel','Case','STRing']
No - that doesn't count. You didn't say "neener neener". ;^)

Jim Davis
Jul 22 '06 #10
JRS: In article <rNcwg.15164$Nv.12737@fed1read10>, dated Fri, 21 Jul
2006 15:44:40 remote, seen in news:comp.lang.javascript, Jeremy
<je****@pinacol.composted :
>Dr John Stockton wrote:
>>
But this : "camelCaseString".match(/(\b|[A-Z])[a-z]*/g)
returns ['camel','Case','String']

And this : "camelCaseSTRing".match(/(\b|[A-Z]+)[a-z]*/g)
returns ['camel','Case','STRing']

What does \b do? Never seen that before.

\b Matches a word boundary, that is, the position between a word and a
space. For example, 'er\b' matches the 'er' in "never" but not the 'er'
in "verb".

\b Matches a word boundary, such as a space. (Not to be confused with
[\b].) For example, /\bn\w/ matches the 'no' in "noonday";/\wy\b/
matches the 'ly' in "possibly yesterday."
Note that the documentations quoted above are inadequate, not really
defining "word boundary".

A Web search should find lists of RegExp parts.

<FAQENTRYRegExps are often asked about, but the FAQ is little help
(two links in 4.16); AFAIK, the Notes do not cover them (IMHO, the FAQ
should have a link to EACH of the Motes).

Try the links in <URL:http://www.merlyn.demon.co.uk/js-valid.htm>.
--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 22 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Stu Cazzo | last post: by
2 posts views Thread by John Benson | last post: by
5 posts views Thread by Vamsi | last post: by
3 posts views Thread by John Salerno | last post: by
6 posts views Thread by Saurabh | last post: by
3 posts views Thread by shapper | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.