split camelcase string into array of words words

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

  1. pantagruel

    pantagruel Guest


    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.

    pantagruel, Jul 21, 2006
    1. Advertisements

  2. pantagruel

    Lee Guest

    pantagruel said:
    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.

    <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++) {
    // for demo purposes return with space delimiters
    return token.join(" ");
    <script type="text/javascript">

    Lee, Jul 21, 2006
    1. Advertisements

  3. pantagruel

    Jeremy Guest


    returns array:

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

    Seems like what you want :)

    Jeremy, Jul 21, 2006
  4. pantagruel

    pantagruel Guest

    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.

    Bryan Rasmussen
    pantagruel, Jul 21, 2006
  5. JRS: In article <>, dated
    Fri, 21 Jul 2006 13:35:43 remote, seen in Jim

    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']
    Dr John Stockton, Jul 21, 2006
  6. pantagruel

    Jeremy Guest

    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, 2006
  7. pantagruel

    Jeremy Guest

    Besides, by tweaking the regex a little you can account for


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

    Jeremy Guest

    What does \b do? Never seen that before.

    Jeremy, Jul 21, 2006
  9. JRS: In article <[email protected]>, dated Fri, 21 Jul
    2006 15:44:40 remote, seen in Jeremy

    \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>.
    Dr John Stockton, Jul 22, 2006
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.