split camelcase string into array of words words


P

pantagruel

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

Advertisements

L

Lee

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+(token[i+1]||"");
}
// for demo purposes return with space delimiters
return token.join(" ");
}
</script>
</head>
<body>
<script type="text/javascript">
alert(unCamel("allowsRunsOfUppercaseEvenAtTheEndForExampleHTML"));
</script>
</body>
</html>


--
 
J

Jeremy

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.


Thanks.

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

returns array:

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

Seems like what you want :)

Jeremy
 
P

pantagruel

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
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated
Fri, 21 Jul 2006 13:35:43 remote, seen in Jim
Davis said:
Jeremy said:
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']
 
J

Jeremy

Jim said:
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
 
Ad

Advertisements

J

Jeremy

Jeremy said:
Jim said:
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
 
J

Jeremy

Dr said:
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
 
Ad

Advertisements

D

Dr John Stockton

JRS: In article <[email protected]>, dated Fri, 21 Jul
2006 15:44:40 remote, seen in Jeremy
Dr said:
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>.
 

Top