Convert camelCase and PascalCase to Title Case

Bent spoke design wheels — feasible?

PhD student with mental health issues and bad performance

Is there any word or phrase for negative bearing?

Past participle agreement with the subject in the case of pronominal verbs

What's the most polite way to tell a manager "shut up and let me work"?

Pros and cons of writing a book review?

What happens if you do emergency landing on a US base in middle of the ocean?

What do we gain with higher order logics?

Is it a problem that pull requests are approved without any comments

Anatomically Correct Swarm of Rodents

Movie where a boy is transported into the future by an alien spaceship

Do manufacturers try make their components as close to ideal ones as possible?

Explain Ant-Man's "not it" scene from Avengers: Endgame

Java 8: How to convert String to Map<String,List<String>>?

What does War Machine's "Canopy! Canopy!" line mean in "Avengers: Endgame"?

I wrote a scene that the majority of my readers loved. How do I get back to that place while writing my new book?

Does any lore text explain why the planes of Acheron, Gehenna, and Carceri are the alignment they are?

The term for the person/group a political party aligns themselves with to appear concerned about the general public

How bad would a partial hash leak be, realistically?

When writing an error prompt, should we end the sentence with a exclamation mark or a dot?

Is the decompression of compressed and encrypted data without decryption also theoretically impossible?

Align text within align

3 as a Sum of 3 Pan Digital Expressions

Word for a small burst of laughter that can't be held back



Convert camelCase and PascalCase to Title Case







.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








9












$begingroup$


I'm trying to take camelCase and PascalCase strings into a function and spit them out as Title Case. This function also needs to be able to handle odd PascalCase strings with capitalized abbreviations such as "CDReceiverBox" and return a readable string - "CD Receiver Box".



My current working solution:



function splitCamelCase(camelCaseString) 
const result = camelCaseString
.replace(/([A-Z][a-z])/g, " $1")
.replace(/([A-Z]+)/g, " $1")
.replace(/ +/g, " ")
.replace(/^ +/g, "");

return result.charAt(0).toUpperCase() + result.slice(1);



I would like to condense the amount of replace statements I'm using by at least combining the first two replace statements and the last two together since they are semi similar. The more concise I can make this the better.



CodePen: https://codepen.io/andrewgarrison/pen/dEQrMy










share|improve this question









New contributor



Andrew Garrison is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






$endgroup$


















    9












    $begingroup$


    I'm trying to take camelCase and PascalCase strings into a function and spit them out as Title Case. This function also needs to be able to handle odd PascalCase strings with capitalized abbreviations such as "CDReceiverBox" and return a readable string - "CD Receiver Box".



    My current working solution:



    function splitCamelCase(camelCaseString) 
    const result = camelCaseString
    .replace(/([A-Z][a-z])/g, " $1")
    .replace(/([A-Z]+)/g, " $1")
    .replace(/ +/g, " ")
    .replace(/^ +/g, "");

    return result.charAt(0).toUpperCase() + result.slice(1);



    I would like to condense the amount of replace statements I'm using by at least combining the first two replace statements and the last two together since they are semi similar. The more concise I can make this the better.



    CodePen: https://codepen.io/andrewgarrison/pen/dEQrMy










    share|improve this question









    New contributor



    Andrew Garrison is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






    $endgroup$














      9












      9








      9


      1



      $begingroup$


      I'm trying to take camelCase and PascalCase strings into a function and spit them out as Title Case. This function also needs to be able to handle odd PascalCase strings with capitalized abbreviations such as "CDReceiverBox" and return a readable string - "CD Receiver Box".



      My current working solution:



      function splitCamelCase(camelCaseString) 
      const result = camelCaseString
      .replace(/([A-Z][a-z])/g, " $1")
      .replace(/([A-Z]+)/g, " $1")
      .replace(/ +/g, " ")
      .replace(/^ +/g, "");

      return result.charAt(0).toUpperCase() + result.slice(1);



      I would like to condense the amount of replace statements I'm using by at least combining the first two replace statements and the last two together since they are semi similar. The more concise I can make this the better.



      CodePen: https://codepen.io/andrewgarrison/pen/dEQrMy










      share|improve this question









      New contributor



      Andrew Garrison is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      $endgroup$




      I'm trying to take camelCase and PascalCase strings into a function and spit them out as Title Case. This function also needs to be able to handle odd PascalCase strings with capitalized abbreviations such as "CDReceiverBox" and return a readable string - "CD Receiver Box".



      My current working solution:



      function splitCamelCase(camelCaseString) 
      const result = camelCaseString
      .replace(/([A-Z][a-z])/g, " $1")
      .replace(/([A-Z]+)/g, " $1")
      .replace(/ +/g, " ")
      .replace(/^ +/g, "");

      return result.charAt(0).toUpperCase() + result.slice(1);



      I would like to condense the amount of replace statements I'm using by at least combining the first two replace statements and the last two together since they are semi similar. The more concise I can make this the better.



      CodePen: https://codepen.io/andrewgarrison/pen/dEQrMy







      javascript strings regex






      share|improve this question









      New contributor



      Andrew Garrison is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.










      share|improve this question









      New contributor



      Andrew Garrison is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      share|improve this question




      share|improve this question








      edited 5 hours ago









      200_success

      133k20160429




      133k20160429






      New contributor



      Andrew Garrison is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      asked 8 hours ago









      Andrew GarrisonAndrew Garrison

      484




      484




      New contributor



      Andrew Garrison is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




      New contributor




      Andrew Garrison is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          2 Answers
          2






          active

          oldest

          votes


















          4












          $begingroup$

          I like your solution quite a bit. It's clear, easy to read and I don't see any bugs.



          There are many ways to condense the replace calls as you mention, but I think you're at a point where such changes can easily have a disproportionate impact on readability. That's good--it means the code is already pretty optimal from that standpoint.



          For example, here's a one-shot replace using alternation, but its merit is debatable:



          const splitCamelCase = s => s.replace(
          /^[a-z]|^([A-Z]+)(?=[A-Z]|$)|([A-Z])+(?=[A-Z]|$)|([A-Z])(?=[a-z]+)/g,
          m => " " + m.toUpperCase()
          ).trim()
          ;


          The idea here is to enumerate each scenario, join the patterns with |s, and provide an arrow function to handle the addition of a space and a capital letter for each match.



          With the two extremes in mind, I prefer a balanced approach such as:



          const splitCamelCase = s =>
          s.replace(/([A-Z][a-z])/g, " $1")
          .replace(/s*([A-Z]+)/g, " $1")
          .replace(/./, m => m.toUpperCase())
          .trim()
          ;


          or perhaps



          const splitCamelCase = s =>
          s.replace(/([A-Z][a-z]|[A-Z]+(?=[A-Z]|$))/g, " $1")
          .replace(/./, m => m.toUpperCase())
          .trim()
          ;


          These should offer ideas as far as how far you want to go in making the succinctness versus readability tradeoff. But, failing the possibility of a shortcut I might have overlooked, keeping your code basically as-is seems like a fine option to me.



          If it's performance you're after in reducing replace calls, there's no guarantee that fewer calls will translate into better performance. Under the hood, the regex engine may make more passes to compensate; you can benchmark and tweak using a debugger like regex101. For performance, it's likely best to avoid regex entirely and write a single-pass loop by hand.



          Here's a test runner:






          const splitCamelCase = s =>
          s.replace(/([A-Z][a-z]|[A-Z]+(?=[A-Z]|$))/g, " $1")
          .replace(/./, m => m.toUpperCase())
          .trim()
          ;

          [
          "AAABbbbbCcDddEEFffGGHhIiJ",
          "AaBbCcDDEeFGgHHHH",
          "CDBoomBoxAAAABbbbCCC",
          "CDBoomBox",
          "camelCase",
          "camel",
          "Camel",
          "c",
          "C",
          "Aa",
          "AA",
          "aa",
          "AAA",
          "aB",
          "aBC",
          "aBCc",
          "",
          ].forEach(test =>
          console.log(
          splitCamelCaseOriginal(test) === splitCamelCase(test)
          ? `'$test' -> '$splitCamelCase(test)'`
          : "TEST FAILED"
          )
          );

          function splitCamelCaseOriginal(camelCaseString)
          const result = camelCaseString
          .replace(/([A-Z][a-z])/g, " $1")
          .replace(/([A-Z]+)/g, " $1")
          .replace(/ +/g, " ")
          .replace(/^ +/g, "");

          return result.charAt(0).toUpperCase() + result.slice(1);








          share|improve this answer











          $endgroup$












          • $begingroup$
            FWIW, your code gives different results from the OP's for inputs like aB, aBC or aBCd that start with a lowercase letter followed by an uppercase letter. That's the one situation where it matters whether you capitalize the first letter before or after adding the spaces.
            $endgroup$
            – Ilmari Karonen
            4 hours ago






          • 1




            $begingroup$
            Good catch, fixed.
            $endgroup$
            – ggorlen
            3 hours ago


















          4












          $begingroup$

          You could save one .replace() call by replacing the last two with:



          .replace(/(^| ) +/g, "$1")


          which both removes leading spaces and collapses multiple consecutive spaces to one anywhere else in the string. However, I'm not 100% sure that you should, since it's not really clear which way is more efficient in practice, and your way seems more readable anyway.



          If you do keep the two calls separate, however, you should optimize the first regexp to / +/g (with two spaces before to + sign) to avoid unnecessarily matching and replacing single spaces. Also, swapping the order of the last two calls could improve performance slightly in cases where the only extra spaces to be removed are at the beginning of the string.






          share|improve this answer









          $endgroup$













            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "196"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );






            Andrew Garrison is a new contributor. Be nice, and check out our Code of Conduct.









            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f221419%2fconvert-camelcase-and-pascalcase-to-title-case%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            4












            $begingroup$

            I like your solution quite a bit. It's clear, easy to read and I don't see any bugs.



            There are many ways to condense the replace calls as you mention, but I think you're at a point where such changes can easily have a disproportionate impact on readability. That's good--it means the code is already pretty optimal from that standpoint.



            For example, here's a one-shot replace using alternation, but its merit is debatable:



            const splitCamelCase = s => s.replace(
            /^[a-z]|^([A-Z]+)(?=[A-Z]|$)|([A-Z])+(?=[A-Z]|$)|([A-Z])(?=[a-z]+)/g,
            m => " " + m.toUpperCase()
            ).trim()
            ;


            The idea here is to enumerate each scenario, join the patterns with |s, and provide an arrow function to handle the addition of a space and a capital letter for each match.



            With the two extremes in mind, I prefer a balanced approach such as:



            const splitCamelCase = s =>
            s.replace(/([A-Z][a-z])/g, " $1")
            .replace(/s*([A-Z]+)/g, " $1")
            .replace(/./, m => m.toUpperCase())
            .trim()
            ;


            or perhaps



            const splitCamelCase = s =>
            s.replace(/([A-Z][a-z]|[A-Z]+(?=[A-Z]|$))/g, " $1")
            .replace(/./, m => m.toUpperCase())
            .trim()
            ;


            These should offer ideas as far as how far you want to go in making the succinctness versus readability tradeoff. But, failing the possibility of a shortcut I might have overlooked, keeping your code basically as-is seems like a fine option to me.



            If it's performance you're after in reducing replace calls, there's no guarantee that fewer calls will translate into better performance. Under the hood, the regex engine may make more passes to compensate; you can benchmark and tweak using a debugger like regex101. For performance, it's likely best to avoid regex entirely and write a single-pass loop by hand.



            Here's a test runner:






            const splitCamelCase = s =>
            s.replace(/([A-Z][a-z]|[A-Z]+(?=[A-Z]|$))/g, " $1")
            .replace(/./, m => m.toUpperCase())
            .trim()
            ;

            [
            "AAABbbbbCcDddEEFffGGHhIiJ",
            "AaBbCcDDEeFGgHHHH",
            "CDBoomBoxAAAABbbbCCC",
            "CDBoomBox",
            "camelCase",
            "camel",
            "Camel",
            "c",
            "C",
            "Aa",
            "AA",
            "aa",
            "AAA",
            "aB",
            "aBC",
            "aBCc",
            "",
            ].forEach(test =>
            console.log(
            splitCamelCaseOriginal(test) === splitCamelCase(test)
            ? `'$test' -> '$splitCamelCase(test)'`
            : "TEST FAILED"
            )
            );

            function splitCamelCaseOriginal(camelCaseString)
            const result = camelCaseString
            .replace(/([A-Z][a-z])/g, " $1")
            .replace(/([A-Z]+)/g, " $1")
            .replace(/ +/g, " ")
            .replace(/^ +/g, "");

            return result.charAt(0).toUpperCase() + result.slice(1);








            share|improve this answer











            $endgroup$












            • $begingroup$
              FWIW, your code gives different results from the OP's for inputs like aB, aBC or aBCd that start with a lowercase letter followed by an uppercase letter. That's the one situation where it matters whether you capitalize the first letter before or after adding the spaces.
              $endgroup$
              – Ilmari Karonen
              4 hours ago






            • 1




              $begingroup$
              Good catch, fixed.
              $endgroup$
              – ggorlen
              3 hours ago















            4












            $begingroup$

            I like your solution quite a bit. It's clear, easy to read and I don't see any bugs.



            There are many ways to condense the replace calls as you mention, but I think you're at a point where such changes can easily have a disproportionate impact on readability. That's good--it means the code is already pretty optimal from that standpoint.



            For example, here's a one-shot replace using alternation, but its merit is debatable:



            const splitCamelCase = s => s.replace(
            /^[a-z]|^([A-Z]+)(?=[A-Z]|$)|([A-Z])+(?=[A-Z]|$)|([A-Z])(?=[a-z]+)/g,
            m => " " + m.toUpperCase()
            ).trim()
            ;


            The idea here is to enumerate each scenario, join the patterns with |s, and provide an arrow function to handle the addition of a space and a capital letter for each match.



            With the two extremes in mind, I prefer a balanced approach such as:



            const splitCamelCase = s =>
            s.replace(/([A-Z][a-z])/g, " $1")
            .replace(/s*([A-Z]+)/g, " $1")
            .replace(/./, m => m.toUpperCase())
            .trim()
            ;


            or perhaps



            const splitCamelCase = s =>
            s.replace(/([A-Z][a-z]|[A-Z]+(?=[A-Z]|$))/g, " $1")
            .replace(/./, m => m.toUpperCase())
            .trim()
            ;


            These should offer ideas as far as how far you want to go in making the succinctness versus readability tradeoff. But, failing the possibility of a shortcut I might have overlooked, keeping your code basically as-is seems like a fine option to me.



            If it's performance you're after in reducing replace calls, there's no guarantee that fewer calls will translate into better performance. Under the hood, the regex engine may make more passes to compensate; you can benchmark and tweak using a debugger like regex101. For performance, it's likely best to avoid regex entirely and write a single-pass loop by hand.



            Here's a test runner:






            const splitCamelCase = s =>
            s.replace(/([A-Z][a-z]|[A-Z]+(?=[A-Z]|$))/g, " $1")
            .replace(/./, m => m.toUpperCase())
            .trim()
            ;

            [
            "AAABbbbbCcDddEEFffGGHhIiJ",
            "AaBbCcDDEeFGgHHHH",
            "CDBoomBoxAAAABbbbCCC",
            "CDBoomBox",
            "camelCase",
            "camel",
            "Camel",
            "c",
            "C",
            "Aa",
            "AA",
            "aa",
            "AAA",
            "aB",
            "aBC",
            "aBCc",
            "",
            ].forEach(test =>
            console.log(
            splitCamelCaseOriginal(test) === splitCamelCase(test)
            ? `'$test' -> '$splitCamelCase(test)'`
            : "TEST FAILED"
            )
            );

            function splitCamelCaseOriginal(camelCaseString)
            const result = camelCaseString
            .replace(/([A-Z][a-z])/g, " $1")
            .replace(/([A-Z]+)/g, " $1")
            .replace(/ +/g, " ")
            .replace(/^ +/g, "");

            return result.charAt(0).toUpperCase() + result.slice(1);








            share|improve this answer











            $endgroup$












            • $begingroup$
              FWIW, your code gives different results from the OP's for inputs like aB, aBC or aBCd that start with a lowercase letter followed by an uppercase letter. That's the one situation where it matters whether you capitalize the first letter before or after adding the spaces.
              $endgroup$
              – Ilmari Karonen
              4 hours ago






            • 1




              $begingroup$
              Good catch, fixed.
              $endgroup$
              – ggorlen
              3 hours ago













            4












            4








            4





            $begingroup$

            I like your solution quite a bit. It's clear, easy to read and I don't see any bugs.



            There are many ways to condense the replace calls as you mention, but I think you're at a point where such changes can easily have a disproportionate impact on readability. That's good--it means the code is already pretty optimal from that standpoint.



            For example, here's a one-shot replace using alternation, but its merit is debatable:



            const splitCamelCase = s => s.replace(
            /^[a-z]|^([A-Z]+)(?=[A-Z]|$)|([A-Z])+(?=[A-Z]|$)|([A-Z])(?=[a-z]+)/g,
            m => " " + m.toUpperCase()
            ).trim()
            ;


            The idea here is to enumerate each scenario, join the patterns with |s, and provide an arrow function to handle the addition of a space and a capital letter for each match.



            With the two extremes in mind, I prefer a balanced approach such as:



            const splitCamelCase = s =>
            s.replace(/([A-Z][a-z])/g, " $1")
            .replace(/s*([A-Z]+)/g, " $1")
            .replace(/./, m => m.toUpperCase())
            .trim()
            ;


            or perhaps



            const splitCamelCase = s =>
            s.replace(/([A-Z][a-z]|[A-Z]+(?=[A-Z]|$))/g, " $1")
            .replace(/./, m => m.toUpperCase())
            .trim()
            ;


            These should offer ideas as far as how far you want to go in making the succinctness versus readability tradeoff. But, failing the possibility of a shortcut I might have overlooked, keeping your code basically as-is seems like a fine option to me.



            If it's performance you're after in reducing replace calls, there's no guarantee that fewer calls will translate into better performance. Under the hood, the regex engine may make more passes to compensate; you can benchmark and tweak using a debugger like regex101. For performance, it's likely best to avoid regex entirely and write a single-pass loop by hand.



            Here's a test runner:






            const splitCamelCase = s =>
            s.replace(/([A-Z][a-z]|[A-Z]+(?=[A-Z]|$))/g, " $1")
            .replace(/./, m => m.toUpperCase())
            .trim()
            ;

            [
            "AAABbbbbCcDddEEFffGGHhIiJ",
            "AaBbCcDDEeFGgHHHH",
            "CDBoomBoxAAAABbbbCCC",
            "CDBoomBox",
            "camelCase",
            "camel",
            "Camel",
            "c",
            "C",
            "Aa",
            "AA",
            "aa",
            "AAA",
            "aB",
            "aBC",
            "aBCc",
            "",
            ].forEach(test =>
            console.log(
            splitCamelCaseOriginal(test) === splitCamelCase(test)
            ? `'$test' -> '$splitCamelCase(test)'`
            : "TEST FAILED"
            )
            );

            function splitCamelCaseOriginal(camelCaseString)
            const result = camelCaseString
            .replace(/([A-Z][a-z])/g, " $1")
            .replace(/([A-Z]+)/g, " $1")
            .replace(/ +/g, " ")
            .replace(/^ +/g, "");

            return result.charAt(0).toUpperCase() + result.slice(1);








            share|improve this answer











            $endgroup$



            I like your solution quite a bit. It's clear, easy to read and I don't see any bugs.



            There are many ways to condense the replace calls as you mention, but I think you're at a point where such changes can easily have a disproportionate impact on readability. That's good--it means the code is already pretty optimal from that standpoint.



            For example, here's a one-shot replace using alternation, but its merit is debatable:



            const splitCamelCase = s => s.replace(
            /^[a-z]|^([A-Z]+)(?=[A-Z]|$)|([A-Z])+(?=[A-Z]|$)|([A-Z])(?=[a-z]+)/g,
            m => " " + m.toUpperCase()
            ).trim()
            ;


            The idea here is to enumerate each scenario, join the patterns with |s, and provide an arrow function to handle the addition of a space and a capital letter for each match.



            With the two extremes in mind, I prefer a balanced approach such as:



            const splitCamelCase = s =>
            s.replace(/([A-Z][a-z])/g, " $1")
            .replace(/s*([A-Z]+)/g, " $1")
            .replace(/./, m => m.toUpperCase())
            .trim()
            ;


            or perhaps



            const splitCamelCase = s =>
            s.replace(/([A-Z][a-z]|[A-Z]+(?=[A-Z]|$))/g, " $1")
            .replace(/./, m => m.toUpperCase())
            .trim()
            ;


            These should offer ideas as far as how far you want to go in making the succinctness versus readability tradeoff. But, failing the possibility of a shortcut I might have overlooked, keeping your code basically as-is seems like a fine option to me.



            If it's performance you're after in reducing replace calls, there's no guarantee that fewer calls will translate into better performance. Under the hood, the regex engine may make more passes to compensate; you can benchmark and tweak using a debugger like regex101. For performance, it's likely best to avoid regex entirely and write a single-pass loop by hand.



            Here's a test runner:






            const splitCamelCase = s =>
            s.replace(/([A-Z][a-z]|[A-Z]+(?=[A-Z]|$))/g, " $1")
            .replace(/./, m => m.toUpperCase())
            .trim()
            ;

            [
            "AAABbbbbCcDddEEFffGGHhIiJ",
            "AaBbCcDDEeFGgHHHH",
            "CDBoomBoxAAAABbbbCCC",
            "CDBoomBox",
            "camelCase",
            "camel",
            "Camel",
            "c",
            "C",
            "Aa",
            "AA",
            "aa",
            "AAA",
            "aB",
            "aBC",
            "aBCc",
            "",
            ].forEach(test =>
            console.log(
            splitCamelCaseOriginal(test) === splitCamelCase(test)
            ? `'$test' -> '$splitCamelCase(test)'`
            : "TEST FAILED"
            )
            );

            function splitCamelCaseOriginal(camelCaseString)
            const result = camelCaseString
            .replace(/([A-Z][a-z])/g, " $1")
            .replace(/([A-Z]+)/g, " $1")
            .replace(/ +/g, " ")
            .replace(/^ +/g, "");

            return result.charAt(0).toUpperCase() + result.slice(1);








            const splitCamelCase = s =>
            s.replace(/([A-Z][a-z]|[A-Z]+(?=[A-Z]|$))/g, " $1")
            .replace(/./, m => m.toUpperCase())
            .trim()
            ;

            [
            "AAABbbbbCcDddEEFffGGHhIiJ",
            "AaBbCcDDEeFGgHHHH",
            "CDBoomBoxAAAABbbbCCC",
            "CDBoomBox",
            "camelCase",
            "camel",
            "Camel",
            "c",
            "C",
            "Aa",
            "AA",
            "aa",
            "AAA",
            "aB",
            "aBC",
            "aBCc",
            "",
            ].forEach(test =>
            console.log(
            splitCamelCaseOriginal(test) === splitCamelCase(test)
            ? `'$test' -> '$splitCamelCase(test)'`
            : "TEST FAILED"
            )
            );

            function splitCamelCaseOriginal(camelCaseString)
            const result = camelCaseString
            .replace(/([A-Z][a-z])/g, " $1")
            .replace(/([A-Z]+)/g, " $1")
            .replace(/ +/g, " ")
            .replace(/^ +/g, "");

            return result.charAt(0).toUpperCase() + result.slice(1);





            const splitCamelCase = s =>
            s.replace(/([A-Z][a-z]|[A-Z]+(?=[A-Z]|$))/g, " $1")
            .replace(/./, m => m.toUpperCase())
            .trim()
            ;

            [
            "AAABbbbbCcDddEEFffGGHhIiJ",
            "AaBbCcDDEeFGgHHHH",
            "CDBoomBoxAAAABbbbCCC",
            "CDBoomBox",
            "camelCase",
            "camel",
            "Camel",
            "c",
            "C",
            "Aa",
            "AA",
            "aa",
            "AAA",
            "aB",
            "aBC",
            "aBCc",
            "",
            ].forEach(test =>
            console.log(
            splitCamelCaseOriginal(test) === splitCamelCase(test)
            ? `'$test' -> '$splitCamelCase(test)'`
            : "TEST FAILED"
            )
            );

            function splitCamelCaseOriginal(camelCaseString)
            const result = camelCaseString
            .replace(/([A-Z][a-z])/g, " $1")
            .replace(/([A-Z]+)/g, " $1")
            .replace(/ +/g, " ")
            .replace(/^ +/g, "");

            return result.charAt(0).toUpperCase() + result.slice(1);






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 3 hours ago

























            answered 6 hours ago









            ggorlenggorlen

            8181314




            8181314











            • $begingroup$
              FWIW, your code gives different results from the OP's for inputs like aB, aBC or aBCd that start with a lowercase letter followed by an uppercase letter. That's the one situation where it matters whether you capitalize the first letter before or after adding the spaces.
              $endgroup$
              – Ilmari Karonen
              4 hours ago






            • 1




              $begingroup$
              Good catch, fixed.
              $endgroup$
              – ggorlen
              3 hours ago
















            • $begingroup$
              FWIW, your code gives different results from the OP's for inputs like aB, aBC or aBCd that start with a lowercase letter followed by an uppercase letter. That's the one situation where it matters whether you capitalize the first letter before or after adding the spaces.
              $endgroup$
              – Ilmari Karonen
              4 hours ago






            • 1




              $begingroup$
              Good catch, fixed.
              $endgroup$
              – ggorlen
              3 hours ago















            $begingroup$
            FWIW, your code gives different results from the OP's for inputs like aB, aBC or aBCd that start with a lowercase letter followed by an uppercase letter. That's the one situation where it matters whether you capitalize the first letter before or after adding the spaces.
            $endgroup$
            – Ilmari Karonen
            4 hours ago




            $begingroup$
            FWIW, your code gives different results from the OP's for inputs like aB, aBC or aBCd that start with a lowercase letter followed by an uppercase letter. That's the one situation where it matters whether you capitalize the first letter before or after adding the spaces.
            $endgroup$
            – Ilmari Karonen
            4 hours ago




            1




            1




            $begingroup$
            Good catch, fixed.
            $endgroup$
            – ggorlen
            3 hours ago




            $begingroup$
            Good catch, fixed.
            $endgroup$
            – ggorlen
            3 hours ago













            4












            $begingroup$

            You could save one .replace() call by replacing the last two with:



            .replace(/(^| ) +/g, "$1")


            which both removes leading spaces and collapses multiple consecutive spaces to one anywhere else in the string. However, I'm not 100% sure that you should, since it's not really clear which way is more efficient in practice, and your way seems more readable anyway.



            If you do keep the two calls separate, however, you should optimize the first regexp to / +/g (with two spaces before to + sign) to avoid unnecessarily matching and replacing single spaces. Also, swapping the order of the last two calls could improve performance slightly in cases where the only extra spaces to be removed are at the beginning of the string.






            share|improve this answer









            $endgroup$

















              4












              $begingroup$

              You could save one .replace() call by replacing the last two with:



              .replace(/(^| ) +/g, "$1")


              which both removes leading spaces and collapses multiple consecutive spaces to one anywhere else in the string. However, I'm not 100% sure that you should, since it's not really clear which way is more efficient in practice, and your way seems more readable anyway.



              If you do keep the two calls separate, however, you should optimize the first regexp to / +/g (with two spaces before to + sign) to avoid unnecessarily matching and replacing single spaces. Also, swapping the order of the last two calls could improve performance slightly in cases where the only extra spaces to be removed are at the beginning of the string.






              share|improve this answer









              $endgroup$















                4












                4








                4





                $begingroup$

                You could save one .replace() call by replacing the last two with:



                .replace(/(^| ) +/g, "$1")


                which both removes leading spaces and collapses multiple consecutive spaces to one anywhere else in the string. However, I'm not 100% sure that you should, since it's not really clear which way is more efficient in practice, and your way seems more readable anyway.



                If you do keep the two calls separate, however, you should optimize the first regexp to / +/g (with two spaces before to + sign) to avoid unnecessarily matching and replacing single spaces. Also, swapping the order of the last two calls could improve performance slightly in cases where the only extra spaces to be removed are at the beginning of the string.






                share|improve this answer









                $endgroup$



                You could save one .replace() call by replacing the last two with:



                .replace(/(^| ) +/g, "$1")


                which both removes leading spaces and collapses multiple consecutive spaces to one anywhere else in the string. However, I'm not 100% sure that you should, since it's not really clear which way is more efficient in practice, and your way seems more readable anyway.



                If you do keep the two calls separate, however, you should optimize the first regexp to / +/g (with two spaces before to + sign) to avoid unnecessarily matching and replacing single spaces. Also, swapping the order of the last two calls could improve performance slightly in cases where the only extra spaces to be removed are at the beginning of the string.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 7 hours ago









                Ilmari KaronenIlmari Karonen

                2,011915




                2,011915




















                    Andrew Garrison is a new contributor. Be nice, and check out our Code of Conduct.









                    draft saved

                    draft discarded


















                    Andrew Garrison is a new contributor. Be nice, and check out our Code of Conduct.












                    Andrew Garrison is a new contributor. Be nice, and check out our Code of Conduct.











                    Andrew Garrison is a new contributor. Be nice, and check out our Code of Conduct.














                    Thanks for contributing an answer to Code Review Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    Use MathJax to format equations. MathJax reference.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f221419%2fconvert-camelcase-and-pascalcase-to-title-case%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

                    Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

                    François Viète Contents Biography Work and thought Bibliography See also Notes Further reading External links Navigation menup. 21Google Bookspp. 75–77Google BooksDe thou (from University of Saint Andrews)ArchivedGoogle BooksGoogle BooksGoogle BooksGoogle booksGoogle Bookscc-parthenay.frL'histoire universelle (fr)Universal History (en)ArchivedAdsabs.harvard.eduPagesperso-orange.frArchive.orgChikara Sasaki. Descartes' mathematical thought p.259Google BooksGoogle BooksGoogle Bookspp. 152 and onwardGoogle BooksGoogle BooksScribd.comGoogle Books1257-7979Google BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGallica.bnf.frGoogle BooksGoogle Books"François Viète"Francois Viète: Father of Modern Algebraic NotationThe Lawyer and the GamblerAbout TarporleySite de Jean-Paul GuichardL'algèbre nouvelle"About the Harmonicon"cb120511976(data)1188044800000 0001 0913 5903n82164680ola2013766880073431702w6vt1sb70287374827140948071409480