split camelcase string into array of words words

Discussion in 'Javascript' started by pantagruel, Jul 21, 2006.

  1. pantagruel

    pantagruel Guest

    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.
    pantagruel, Jul 21, 2006
    #1
    1. Advertising

  2. pantagruel

    Lee Guest

    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>


    --
    Lee, Jul 21, 2006
    #2
    1. Advertising

  3. pantagruel

    Jeremy Guest

    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
    Jeremy, Jul 21, 2006
    #3
  4. pantagruel

    pantagruel Guest


    >
    > 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
    pantagruel, Jul 21, 2006
    #4
  5. JRS: In article <>, dated
    Fri, 21 Jul 2006 13:35:43 remote, seen in news:comp.lang.javascript, Jim
    Davis <> posted :
    >"Jeremy" <> 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.
    Dr John Stockton, Jul 21, 2006
    #5
  6. pantagruel

    Jeremy Guest

    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, Jul 21, 2006
    #6
  7. pantagruel

    Jeremy Guest

    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, Jul 21, 2006
    #7
  8. pantagruel

    Jeremy Guest

    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
    Jeremy, Jul 21, 2006
    #8
  9. JRS: In article <rNcwg.15164$Nv.12737@fed1read10>, dated Fri, 21 Jul
    2006 15:44:40 remote, seen in news:comp.lang.javascript, Jeremy
    <> 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.
    Dr John Stockton, Jul 22, 2006
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. qwweeeit

    Split text file into words

    qwweeeit, Mar 8, 2005, in forum: Python
    Replies:
    4
    Views:
    1,328
    Duncan Booth
    Mar 9, 2005
  2. Sam Kong
    Replies:
    5
    Views:
    223
    Rick DeNatale
    Aug 12, 2006
  3. John Butler
    Replies:
    2
    Views:
    151
    Colin Bartlett
    Jun 3, 2010
  4. Stanley Xu
    Replies:
    2
    Views:
    582
    Stanley Xu
    Mar 23, 2011
  5. Robert Oschler
    Replies:
    2
    Views:
    108
    peterS.
    Aug 2, 2005
Loading...

Share This Page