Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > split camelcase string into array of words words

Reply
Thread Tools

split camelcase string into array of words words

 
 
pantagruel
Guest
Posts: n/a
 
      07-21-2006
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.

 
Reply With Quote
 
 
 
 
Lee
Guest
Posts: n/a
 
      07-21-2006
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>


--

 
Reply With Quote
 
 
 
 
Jeremy
Guest
Posts: n/a
 
      07-21-2006
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
 
Reply With Quote
 
pantagruel
Guest
Posts: n/a
 
      07-21-2006

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

 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      07-21-2006
JRS: In article <(E-Mail Removed)>, dated
Fri, 21 Jul 2006 13:35:43 remote, seen in news:comp.lang.javascript, Jim
Davis <(E-Mail Removed)> posted :
>"Jeremy" <(E-Mail Removed)> 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.
 
Reply With Quote
 
Jeremy
Guest
Posts: n/a
 
      07-21-2006
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

 
Reply With Quote
 
Jeremy
Guest
Posts: n/a
 
      07-21-2006
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
 
Reply With Quote
 
Jeremy
Guest
Posts: n/a
 
      07-21-2006
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
 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      07-22-2006
JRS: In article <rNcwg.15164$Nv.12737@fed1read10>, dated Fri, 21 Jul
2006 15:44:40 remote, seen in news:comp.lang.javascript, Jeremy
<(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use String.split to split a mixed encoding string(partencoded in gbk, part encoded in utf-8) Stanley Xu Ruby 2 03-23-2011 02:06 PM
Split text into equal characters but keeping whole words John Butler Ruby 2 06-03-2010 06:48 PM
String#split(/\s+/) vs. String#split(/(\s+)/) Sam Kong Ruby 5 08-12-2006 07:59 PM
Regex help: Split string into words AND double-quoted phrases Robert Oschler Javascript 2 08-02-2005 11:01 AM
Split text file into words qwweeeit Python 4 03-09-2005 03:34 PM



Advertisments