Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Javascript (http://www.velocityreviews.com/forums/f68-javascript.html)
-   -   split camelcase string into array of words words (http://www.velocityreviews.com/forums/t925931-split-camelcase-string-into-array-of-words-words.html)

pantagruel 07-21-2006 11:45 AM

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.


Lee 07-21-2006 02:55 PM

Re: split camelcase string into array of words words
 
pantagruel said:
>
>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.


There may be easier ways to code it, but what actually happens
behind the scenes is going to be just what you described, probably
with even more overhead for processing regular expressions.

You should give priority to using a solution that you understand
and can maintain.

<html>
<head>
<script type="text/javascript">
function unCamel(str) {
// collect runs of upper and lower case characters
var token=str.match(/[^A-Z]+/g);
var upper=str.match(/[A-Z]+/g);
// tack the uppercase characters back into place
for(var i=0;i<upper.length;i++) {
token[i+1]=upper[i]+(token[i+1]||"");
}
// for demo purposes return with space delimiters
return token.join(" ");
}
</script>
</head>
<body>
<script type="text/javascript">
alert(unCamel("allowsRunsOfUppercaseEvenAtTheEndFo rExampleHTML"));
</script>
</body>
</html>


--


Jeremy 07-21-2006 04:55 PM

Re: split camelcase string into array of words words
 
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

pantagruel 07-21-2006 07:36 PM

Re: split camelcase string into array of words words
 

>
> 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


Dr John Stockton 07-21-2006 08:23 PM

Re: split camelcase string into array of words words
 
JRS: In article <ksqdncoVhsTtjVzZnZ2dneKdnZydnZ2d@giganews.com>, dated
Fri, 21 Jul 2006 13:35:43 remote, seen in news:comp.lang.javascript, Jim
Davis <newsmonkey@vboston.com> posted :
>"Jeremy" <jeremy@pinacol.com> wrote 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.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

Jeremy 07-21-2006 08:33 PM

Re: split camelcase string into array of words words
 
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


Jeremy 07-21-2006 08:42 PM

Re: split camelcase string into array of words words
 
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

Jeremy 07-21-2006 10:44 PM

Re: split camelcase string into array of words words
 
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

Dr John Stockton 07-22-2006 07:46 PM

Re: split camelcase string into array of words words
 
JRS: In article <rNcwg.15164$Nv.12737@fed1read10>, dated Fri, 21 Jul
2006 15:44:40 remote, seen in news:comp.lang.javascript, Jeremy
<jeremy@pinacol.com> posted :
>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.

<FAQENTRY> RegExps 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.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.


All times are GMT. The time now is 10:45 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.