Convert camelCase and PascalCase to Title Case

When is the phrase "j'ai bon" used?

How do I gain the trust of other PCs?

Kelvin type connection

Time travel short story where someone from the past follows the travelers back

What does this Swiss black on yellow rectangular traffic sign with a symbol looking like a dart mean?

Is swap gate equivalent to just exchanging the wire of the two qubits?

Definition of 'vrit'

Are there foreign customs agents on US soil?

Derivation of CDF of a function that results in an exponential distribution

Build a scale without computer

Does cooling a potato change the nature of its carbohydrates?

writing a function between sets vertically

Got a new frameset, don't know why I need this split ring collar?

Do details of my undergraduate title matter?

Does knowing the surface area of all faces uniquely determine a tetrahedron?

Is there a polite way to ask about one's ethnicity?

In windows systems, is renaming files functionally similar to deleting them?

Why swap space doesn't get filesystem check at boot time?

Would a 7805 5v regulator drain a 9v battery?

Time at 1G acceleration to travel 100 000 light years

Regex version of strip() - Ch. 7 Automate the Boring Stuff

Bash function: Execute $@ command with each argument in sequence executed separately

Is using Legacy mode is a bad thing to do?

How to recover a single blank shot from a film camera



Convert camelCase and PascalCase to Title Case







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








15












$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











$endgroup$


















    15












    $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











    $endgroup$














      15












      15








      15


      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











      $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















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 31 at 17:39









      200_success

      134k21166439




      134k21166439










      asked May 31 at 14:24









      Andrew GarrisonAndrew Garrison

      785




      785




















          2 Answers
          2






          active

          oldest

          votes


















          10












          $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
            May 31 at 18:20






          • 1




            $begingroup$
            Good catch, fixed.
            $endgroup$
            – ggorlen
            May 31 at 19:08


















          8












          $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) or / 2,/g (which means "two or more spaces"), 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
            );



            );













            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









            10












            $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
              May 31 at 18:20






            • 1




              $begingroup$
              Good catch, fixed.
              $endgroup$
              – ggorlen
              May 31 at 19:08















            10












            $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
              May 31 at 18:20






            • 1




              $begingroup$
              Good catch, fixed.
              $endgroup$
              – ggorlen
              May 31 at 19:08













            10












            10








            10





            $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 May 31 at 19:20

























            answered May 31 at 16:24









            ggorlenggorlen

            8881415




            8881415











            • $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
              May 31 at 18:20






            • 1




              $begingroup$
              Good catch, fixed.
              $endgroup$
              – ggorlen
              May 31 at 19:08
















            • $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
              May 31 at 18:20






            • 1




              $begingroup$
              Good catch, fixed.
              $endgroup$
              – ggorlen
              May 31 at 19:08















            $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
            May 31 at 18:20




            $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
            May 31 at 18:20




            1




            1




            $begingroup$
            Good catch, fixed.
            $endgroup$
            – ggorlen
            May 31 at 19:08




            $begingroup$
            Good catch, fixed.
            $endgroup$
            – ggorlen
            May 31 at 19:08













            8












            $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) or / 2,/g (which means "two or more spaces"), 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$

















              8












              $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) or / 2,/g (which means "two or more spaces"), 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$















                8












                8








                8





                $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) or / 2,/g (which means "two or more spaces"), 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) or / 2,/g (which means "two or more spaces"), 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








                edited Jun 1 at 21:29









                Mitch McMabers

                31




                31










                answered May 31 at 15:04









                Ilmari KaronenIlmari Karonen

                2,071916




                2,071916



























                    draft saved

                    draft discarded
















































                    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

                    Club Baloncesto Breogán Índice Historia | Pavillón | Nome | O Breogán na cultura popular | Xogadores | Adestradores | Presidentes | Palmarés | Historial | Líderes | Notas | Véxase tamén | Menú de navegacióncbbreogan.galCadroGuía oficial da ACB 2009-10, páxina 201Guía oficial ACB 1992, páxina 183. Editorial DB.É de 6.500 espectadores sentados axeitándose á última normativa"Estudiantes Junior, entre as mellores canteiras"o orixinalHemeroteca El Mundo Deportivo, 16 setembro de 1970, páxina 12Historia do BreogánAlfredo Pérez, o último canoneiroHistoria C.B. BreogánHemeroteca de El Mundo DeportivoJimmy Wright, norteamericano do Breogán deixará Lugo por ameazas de morteResultados de Breogán en 1986-87Resultados de Breogán en 1990-91Ficha de Velimir Perasović en acb.comResultados de Breogán en 1994-95Breogán arrasa al Barça. "El Mundo Deportivo", 27 de setembro de 1999, páxina 58CB Breogán - FC BarcelonaA FEB invita a participar nunha nova Liga EuropeaCharlie Bell na prensa estatalMáximos anotadores 2005Tempada 2005-06 : Tódolos Xogadores da Xornada""Non quero pensar nunha man negra, mais pregúntome que está a pasar""o orixinalRaúl López, orgulloso dos xogadores, presume da boa saúde económica do BreogánJulio González confirma que cesa como presidente del BreogánHomenaxe a Lisardo GómezA tempada do rexurdimento celesteEntrevista a Lisardo GómezEl COB dinamita el Pazo para forzar el quinto (69-73)Cafés Candelas, patrocinador del CB Breogán"Suso Lázare, novo presidente do Breogán"o orixinalCafés Candelas Breogán firma el mayor triunfo de la historiaEl Breogán realizará 17 homenajes por su cincuenta aniversario"O Breogán honra ao seu fundador e primeiro presidente"o orixinalMiguel Giao recibiu a homenaxe do PazoHomenaxe aos primeiros gladiadores celestesO home que nos amosa como ver o Breo co corazónTita Franco será homenaxeada polos #50anosdeBreoJulio Vila recibirá unha homenaxe in memoriam polos #50anosdeBreo"O Breogán homenaxeará aos seus aboados máis veteráns"Pechada ovación a «Capi» Sanmartín e Ricardo «Corazón de González»Homenaxe por décadas de informaciónPaco García volve ao Pazo con motivo do 50 aniversario"Resultados y clasificaciones""O Cafés Candelas Breogán, campión da Copa Princesa""O Cafés Candelas Breogán, equipo ACB"C.B. Breogán"Proxecto social"o orixinal"Centros asociados"o orixinalFicha en imdb.comMario Camus trata la recuperación del amor en 'La vieja música', su última película"Páxina web oficial""Club Baloncesto Breogán""C. B. Breogán S.A.D."eehttp://www.fegaba.com

                    Vilaño, A Laracha Índice Patrimonio | Lugares e parroquias | Véxase tamén | Menú de navegación43°14′52″N 8°36′03″O / 43.24775, -8.60070

                    Cegueira Índice Epidemioloxía | Deficiencia visual | Tipos de cegueira | Principais causas de cegueira | Tratamento | Técnicas de adaptación e axudas | Vida dos cegos | Primeiros auxilios | Crenzas respecto das persoas cegas | Crenzas das persoas cegas | O neno deficiente visual | Aspectos psicolóxicos da cegueira | Notas | Véxase tamén | Menú de navegación54.054.154.436928256blindnessDicionario da Real Academia GalegaPortal das Palabras"International Standards: Visual Standards — Aspects and Ranges of Vision Loss with Emphasis on Population Surveys.""Visual impairment and blindness""Presentan un plan para previr a cegueira"o orixinalACCDV Associació Catalana de Cecs i Disminuïts Visuals - PMFTrachoma"Effect of gene therapy on visual function in Leber's congenital amaurosis"1844137110.1056/NEJMoa0802268Cans guía - os mellores amigos dos cegosArquivadoEscola de cans guía para cegos en Mortágua, PortugalArquivado"Tecnología para ciegos y deficientes visuales. Recopilación de recursos gratuitos en la Red""Colorino""‘COL.diesis’, escuchar los sonidos del color""COL.diesis: Transforming Colour into Melody and Implementing the Result in a Colour Sensor Device"o orixinal"Sistema de desarrollo de sinestesia color-sonido para invidentes utilizando un protocolo de audio""Enseñanza táctil - geometría y color. Juegos didácticos para niños ciegos y videntes""Sistema Constanz"L'ocupació laboral dels cecs a l'Estat espanyol està pràcticament equiparada a la de les persones amb visió, entrevista amb Pedro ZuritaONCE (Organización Nacional de Cegos de España)Prevención da cegueiraDescrición de deficiencias visuais (Disc@pnet)Braillín, un boneco atractivo para calquera neno, con ou sen discapacidade, que permite familiarizarse co sistema de escritura e lectura brailleAxudas Técnicas36838ID00897494007150-90057129528256DOID:1432HP:0000618D001766C10.597.751.941.162C97109C0155020