473,387 Members | 1,398 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 11880

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
2
by: John Benson | last post by:
I never cared for CamelCase because a lot of words in English are compounds, and remembering the right CamelCase rendition of them gets difficult. For example, an object attribute named ...
5
by: Vamsi | last post by:
Hi, I am trying a basic opearation of splitting a multiline value to an array of single lines(Actually making Address into AddressLine1, AddressLine2). I used Environment.NewLine in split, I...
3
by: John Salerno | last post by:
This is an example in the book I'm reading: string fullName = " Edward C Koop "; fullName = fullName.Trim(); string names = fullName.Split(' '); string firstName = names; //...
8
by: Dave | last post by:
Greetings, Is there a way I can split a string into an array on a space OR a carriage return? What would the code look like for this? Thanks, -Dave
3
by: Ben | last post by:
Hi I am creating a dynamic function to return a two dimensional array from a delimeted string. The delimited string is like: field1...field2...field3... field1...field2...field3......
6
by: Saurabh | last post by:
Hi everyone, I am looking for some expert advise to get me out of trouble. I am looking for a solution in C# which will allow me to split the below string in the format provided. The...
2
by: pmwhelan | last post by:
Hi I want to split a string into an array and treat quoted phrases as words. I was splitting on a space which worked fine. char delimiterChars = {' '}; string arrValues =...
3
by: shapper | last post by:
Hello, I have a string as follows: string text = "Paris, New York , London,Lisbon,Oslo, Chicago "; I need to get all words with no commas or spaces: "Paris", "New", "York", "London",...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.