Custom regex to format number












2














i have this number
1234567890



this is how i want to display it



1234 567 890



im trying this



console.log('1234567899'.replace(/(d)(?=(d{3})+$)/g, '$1 '));









share|improve this question






















  • which angular version are you using?
    – Abhishek Mani
    Nov 23 '18 at 6:33










  • Can you explain the logic you're looking for? Do you want to omit the first comma when there is only one digit in the first section, or for two as well, or for all 3, or what?
    – CertainPerformance
    Nov 23 '18 at 6:33
















2














i have this number
1234567890



this is how i want to display it



1234 567 890



im trying this



console.log('1234567899'.replace(/(d)(?=(d{3})+$)/g, '$1 '));









share|improve this question






















  • which angular version are you using?
    – Abhishek Mani
    Nov 23 '18 at 6:33










  • Can you explain the logic you're looking for? Do you want to omit the first comma when there is only one digit in the first section, or for two as well, or for all 3, or what?
    – CertainPerformance
    Nov 23 '18 at 6:33














2












2








2







i have this number
1234567890



this is how i want to display it



1234 567 890



im trying this



console.log('1234567899'.replace(/(d)(?=(d{3})+$)/g, '$1 '));









share|improve this question













i have this number
1234567890



this is how i want to display it



1234 567 890



im trying this



console.log('1234567899'.replace(/(d)(?=(d{3})+$)/g, '$1 '));






javascript angularjs regex






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 6:32









Manav

76111




76111












  • which angular version are you using?
    – Abhishek Mani
    Nov 23 '18 at 6:33










  • Can you explain the logic you're looking for? Do you want to omit the first comma when there is only one digit in the first section, or for two as well, or for all 3, or what?
    – CertainPerformance
    Nov 23 '18 at 6:33


















  • which angular version are you using?
    – Abhishek Mani
    Nov 23 '18 at 6:33










  • Can you explain the logic you're looking for? Do you want to omit the first comma when there is only one digit in the first section, or for two as well, or for all 3, or what?
    – CertainPerformance
    Nov 23 '18 at 6:33
















which angular version are you using?
– Abhishek Mani
Nov 23 '18 at 6:33




which angular version are you using?
– Abhishek Mani
Nov 23 '18 at 6:33












Can you explain the logic you're looking for? Do you want to omit the first comma when there is only one digit in the first section, or for two as well, or for all 3, or what?
– CertainPerformance
Nov 23 '18 at 6:33




Can you explain the logic you're looking for? Do you want to omit the first comma when there is only one digit in the first section, or for two as well, or for all 3, or what?
– CertainPerformance
Nov 23 '18 at 6:33












4 Answers
4






active

oldest

votes


















2














One option would be to have an optional group that starts at the beginning of the string and (greedily) matches the number of the leading-digits-without-commas you want to have. Then, instead of replacing with just 1, replace with 12 (the optional group plus the second captured digits):






const format = str => str.replace(
/(^(?:d{1,2}))?(d{1,3})(?=(?:d{3})+$)/g,
// ^^^ change these to change the number of unbroken leading digits
'$1$2 '
);

console.log(format('1234567899'));
console.log(format('01234567899'));
console.log(format('101234567899'));





The above snippet's optional group begins with d{1,2}, which means that there will be between 3 and 5 leading digits, unbroken by commas. To change that quantity, just change the number of repetitions.



The leading group (^(?:d{1,2}))? means: optionally, the beginning of the string, followed by one or two digits.






share|improve this answer





























    0















    • Divide your string into 2 parts.



        1. String contains space after 4 digits



        1. String contains space after 3 digits.




    • Add space after n digits


    Try with this






    var str = "1234567890";
    var temp = str.substring(0,4); // get first 4 digits
    //Add space after 3 digits. You can use same logic to add space after 4 digits as well
    var z = [...str.substring(4)].map((d, i) => i % 3 == 0 ? ' '+d : d).join('').trim();

    //Concatenate both strings
    var result = temp + ' ' + z;

    //Display result
    console.log(temp);
    console.log(z);
    console.log(result);








    share|improve this answer































      0














      Try this regex which ensures the numbers will be grouped in bunches of three except the first digits which can be grouped from four to five.



      Match /(^d{4}|d{3})(?=(d{3})*$)/g and replace with $1



      Here is some sample javascript code,






      console.log('1234567899' + ' --> ' + '1234567899'.replace(/(^d{4}|d{3})(?=(d{3})*$)/g, '$1 '));
      console.log('12345678991' + ' --> ' + '12345678991'.replace(/(^d{4}|d{3})(?=(d{3})*$)/g, '$1 '));
      console.log('123456789912' + ' --> ' + '123456789912'.replace(/(^d{4}|d{3})(?=(d{3})*$)/g, '$1 '));
      console.log('1234567899123' + ' --> ' + '1234567899123'.replace(/(^d{4}|d{3})(?=(d{3})*$)/g, '$1 '));








      share|improve this answer































        0














        You can do with this way.



        let number = '1234567890';
        let result = number.replace(/(d{4})(d{3})(d{3})/, "$1 $2 $3");
        console.warn(result);





        share|improve this answer























          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: "1"
          };
          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: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          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%2fstackoverflow.com%2fquestions%2f53441627%2fcustom-regex-to-format-number%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          One option would be to have an optional group that starts at the beginning of the string and (greedily) matches the number of the leading-digits-without-commas you want to have. Then, instead of replacing with just 1, replace with 12 (the optional group plus the second captured digits):






          const format = str => str.replace(
          /(^(?:d{1,2}))?(d{1,3})(?=(?:d{3})+$)/g,
          // ^^^ change these to change the number of unbroken leading digits
          '$1$2 '
          );

          console.log(format('1234567899'));
          console.log(format('01234567899'));
          console.log(format('101234567899'));





          The above snippet's optional group begins with d{1,2}, which means that there will be between 3 and 5 leading digits, unbroken by commas. To change that quantity, just change the number of repetitions.



          The leading group (^(?:d{1,2}))? means: optionally, the beginning of the string, followed by one or two digits.






          share|improve this answer


























            2














            One option would be to have an optional group that starts at the beginning of the string and (greedily) matches the number of the leading-digits-without-commas you want to have. Then, instead of replacing with just 1, replace with 12 (the optional group plus the second captured digits):






            const format = str => str.replace(
            /(^(?:d{1,2}))?(d{1,3})(?=(?:d{3})+$)/g,
            // ^^^ change these to change the number of unbroken leading digits
            '$1$2 '
            );

            console.log(format('1234567899'));
            console.log(format('01234567899'));
            console.log(format('101234567899'));





            The above snippet's optional group begins with d{1,2}, which means that there will be between 3 and 5 leading digits, unbroken by commas. To change that quantity, just change the number of repetitions.



            The leading group (^(?:d{1,2}))? means: optionally, the beginning of the string, followed by one or two digits.






            share|improve this answer
























              2












              2








              2






              One option would be to have an optional group that starts at the beginning of the string and (greedily) matches the number of the leading-digits-without-commas you want to have. Then, instead of replacing with just 1, replace with 12 (the optional group plus the second captured digits):






              const format = str => str.replace(
              /(^(?:d{1,2}))?(d{1,3})(?=(?:d{3})+$)/g,
              // ^^^ change these to change the number of unbroken leading digits
              '$1$2 '
              );

              console.log(format('1234567899'));
              console.log(format('01234567899'));
              console.log(format('101234567899'));





              The above snippet's optional group begins with d{1,2}, which means that there will be between 3 and 5 leading digits, unbroken by commas. To change that quantity, just change the number of repetitions.



              The leading group (^(?:d{1,2}))? means: optionally, the beginning of the string, followed by one or two digits.






              share|improve this answer












              One option would be to have an optional group that starts at the beginning of the string and (greedily) matches the number of the leading-digits-without-commas you want to have. Then, instead of replacing with just 1, replace with 12 (the optional group plus the second captured digits):






              const format = str => str.replace(
              /(^(?:d{1,2}))?(d{1,3})(?=(?:d{3})+$)/g,
              // ^^^ change these to change the number of unbroken leading digits
              '$1$2 '
              );

              console.log(format('1234567899'));
              console.log(format('01234567899'));
              console.log(format('101234567899'));





              The above snippet's optional group begins with d{1,2}, which means that there will be between 3 and 5 leading digits, unbroken by commas. To change that quantity, just change the number of repetitions.



              The leading group (^(?:d{1,2}))? means: optionally, the beginning of the string, followed by one or two digits.






              const format = str => str.replace(
              /(^(?:d{1,2}))?(d{1,3})(?=(?:d{3})+$)/g,
              // ^^^ change these to change the number of unbroken leading digits
              '$1$2 '
              );

              console.log(format('1234567899'));
              console.log(format('01234567899'));
              console.log(format('101234567899'));





              const format = str => str.replace(
              /(^(?:d{1,2}))?(d{1,3})(?=(?:d{3})+$)/g,
              // ^^^ change these to change the number of unbroken leading digits
              '$1$2 '
              );

              console.log(format('1234567899'));
              console.log(format('01234567899'));
              console.log(format('101234567899'));






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 23 '18 at 6:53









              CertainPerformance

              76.5k143862




              76.5k143862

























                  0















                  • Divide your string into 2 parts.



                      1. String contains space after 4 digits



                      1. String contains space after 3 digits.




                  • Add space after n digits


                  Try with this






                  var str = "1234567890";
                  var temp = str.substring(0,4); // get first 4 digits
                  //Add space after 3 digits. You can use same logic to add space after 4 digits as well
                  var z = [...str.substring(4)].map((d, i) => i % 3 == 0 ? ' '+d : d).join('').trim();

                  //Concatenate both strings
                  var result = temp + ' ' + z;

                  //Display result
                  console.log(temp);
                  console.log(z);
                  console.log(result);








                  share|improve this answer




























                    0















                    • Divide your string into 2 parts.



                        1. String contains space after 4 digits



                        1. String contains space after 3 digits.




                    • Add space after n digits


                    Try with this






                    var str = "1234567890";
                    var temp = str.substring(0,4); // get first 4 digits
                    //Add space after 3 digits. You can use same logic to add space after 4 digits as well
                    var z = [...str.substring(4)].map((d, i) => i % 3 == 0 ? ' '+d : d).join('').trim();

                    //Concatenate both strings
                    var result = temp + ' ' + z;

                    //Display result
                    console.log(temp);
                    console.log(z);
                    console.log(result);








                    share|improve this answer


























                      0












                      0








                      0







                      • Divide your string into 2 parts.



                          1. String contains space after 4 digits



                          1. String contains space after 3 digits.




                      • Add space after n digits


                      Try with this






                      var str = "1234567890";
                      var temp = str.substring(0,4); // get first 4 digits
                      //Add space after 3 digits. You can use same logic to add space after 4 digits as well
                      var z = [...str.substring(4)].map((d, i) => i % 3 == 0 ? ' '+d : d).join('').trim();

                      //Concatenate both strings
                      var result = temp + ' ' + z;

                      //Display result
                      console.log(temp);
                      console.log(z);
                      console.log(result);








                      share|improve this answer















                      • Divide your string into 2 parts.



                          1. String contains space after 4 digits



                          1. String contains space after 3 digits.




                      • Add space after n digits


                      Try with this






                      var str = "1234567890";
                      var temp = str.substring(0,4); // get first 4 digits
                      //Add space after 3 digits. You can use same logic to add space after 4 digits as well
                      var z = [...str.substring(4)].map((d, i) => i % 3 == 0 ? ' '+d : d).join('').trim();

                      //Concatenate both strings
                      var result = temp + ' ' + z;

                      //Display result
                      console.log(temp);
                      console.log(z);
                      console.log(result);








                      var str = "1234567890";
                      var temp = str.substring(0,4); // get first 4 digits
                      //Add space after 3 digits. You can use same logic to add space after 4 digits as well
                      var z = [...str.substring(4)].map((d, i) => i % 3 == 0 ? ' '+d : d).join('').trim();

                      //Concatenate both strings
                      var result = temp + ' ' + z;

                      //Display result
                      console.log(temp);
                      console.log(z);
                      console.log(result);





                      var str = "1234567890";
                      var temp = str.substring(0,4); // get first 4 digits
                      //Add space after 3 digits. You can use same logic to add space after 4 digits as well
                      var z = [...str.substring(4)].map((d, i) => i % 3 == 0 ? ' '+d : d).join('').trim();

                      //Concatenate both strings
                      var result = temp + ' ' + z;

                      //Display result
                      console.log(temp);
                      console.log(z);
                      console.log(result);






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Nov 23 '18 at 7:00

























                      answered Nov 23 '18 at 6:54









                      Prasad Telkikar

                      1,862419




                      1,862419























                          0














                          Try this regex which ensures the numbers will be grouped in bunches of three except the first digits which can be grouped from four to five.



                          Match /(^d{4}|d{3})(?=(d{3})*$)/g and replace with $1



                          Here is some sample javascript code,






                          console.log('1234567899' + ' --> ' + '1234567899'.replace(/(^d{4}|d{3})(?=(d{3})*$)/g, '$1 '));
                          console.log('12345678991' + ' --> ' + '12345678991'.replace(/(^d{4}|d{3})(?=(d{3})*$)/g, '$1 '));
                          console.log('123456789912' + ' --> ' + '123456789912'.replace(/(^d{4}|d{3})(?=(d{3})*$)/g, '$1 '));
                          console.log('1234567899123' + ' --> ' + '1234567899123'.replace(/(^d{4}|d{3})(?=(d{3})*$)/g, '$1 '));








                          share|improve this answer




























                            0














                            Try this regex which ensures the numbers will be grouped in bunches of three except the first digits which can be grouped from four to five.



                            Match /(^d{4}|d{3})(?=(d{3})*$)/g and replace with $1



                            Here is some sample javascript code,






                            console.log('1234567899' + ' --> ' + '1234567899'.replace(/(^d{4}|d{3})(?=(d{3})*$)/g, '$1 '));
                            console.log('12345678991' + ' --> ' + '12345678991'.replace(/(^d{4}|d{3})(?=(d{3})*$)/g, '$1 '));
                            console.log('123456789912' + ' --> ' + '123456789912'.replace(/(^d{4}|d{3})(?=(d{3})*$)/g, '$1 '));
                            console.log('1234567899123' + ' --> ' + '1234567899123'.replace(/(^d{4}|d{3})(?=(d{3})*$)/g, '$1 '));








                            share|improve this answer


























                              0












                              0








                              0






                              Try this regex which ensures the numbers will be grouped in bunches of three except the first digits which can be grouped from four to five.



                              Match /(^d{4}|d{3})(?=(d{3})*$)/g and replace with $1



                              Here is some sample javascript code,






                              console.log('1234567899' + ' --> ' + '1234567899'.replace(/(^d{4}|d{3})(?=(d{3})*$)/g, '$1 '));
                              console.log('12345678991' + ' --> ' + '12345678991'.replace(/(^d{4}|d{3})(?=(d{3})*$)/g, '$1 '));
                              console.log('123456789912' + ' --> ' + '123456789912'.replace(/(^d{4}|d{3})(?=(d{3})*$)/g, '$1 '));
                              console.log('1234567899123' + ' --> ' + '1234567899123'.replace(/(^d{4}|d{3})(?=(d{3})*$)/g, '$1 '));








                              share|improve this answer














                              Try this regex which ensures the numbers will be grouped in bunches of three except the first digits which can be grouped from four to five.



                              Match /(^d{4}|d{3})(?=(d{3})*$)/g and replace with $1



                              Here is some sample javascript code,






                              console.log('1234567899' + ' --> ' + '1234567899'.replace(/(^d{4}|d{3})(?=(d{3})*$)/g, '$1 '));
                              console.log('12345678991' + ' --> ' + '12345678991'.replace(/(^d{4}|d{3})(?=(d{3})*$)/g, '$1 '));
                              console.log('123456789912' + ' --> ' + '123456789912'.replace(/(^d{4}|d{3})(?=(d{3})*$)/g, '$1 '));
                              console.log('1234567899123' + ' --> ' + '1234567899123'.replace(/(^d{4}|d{3})(?=(d{3})*$)/g, '$1 '));








                              console.log('1234567899' + ' --> ' + '1234567899'.replace(/(^d{4}|d{3})(?=(d{3})*$)/g, '$1 '));
                              console.log('12345678991' + ' --> ' + '12345678991'.replace(/(^d{4}|d{3})(?=(d{3})*$)/g, '$1 '));
                              console.log('123456789912' + ' --> ' + '123456789912'.replace(/(^d{4}|d{3})(?=(d{3})*$)/g, '$1 '));
                              console.log('1234567899123' + ' --> ' + '1234567899123'.replace(/(^d{4}|d{3})(?=(d{3})*$)/g, '$1 '));





                              console.log('1234567899' + ' --> ' + '1234567899'.replace(/(^d{4}|d{3})(?=(d{3})*$)/g, '$1 '));
                              console.log('12345678991' + ' --> ' + '12345678991'.replace(/(^d{4}|d{3})(?=(d{3})*$)/g, '$1 '));
                              console.log('123456789912' + ' --> ' + '123456789912'.replace(/(^d{4}|d{3})(?=(d{3})*$)/g, '$1 '));
                              console.log('1234567899123' + ' --> ' + '1234567899123'.replace(/(^d{4}|d{3})(?=(d{3})*$)/g, '$1 '));






                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Nov 23 '18 at 8:06

























                              answered Nov 23 '18 at 7:38









                              Pushpesh Kumar Rajwanshi

                              5,3242827




                              5,3242827























                                  0














                                  You can do with this way.



                                  let number = '1234567890';
                                  let result = number.replace(/(d{4})(d{3})(d{3})/, "$1 $2 $3");
                                  console.warn(result);





                                  share|improve this answer




























                                    0














                                    You can do with this way.



                                    let number = '1234567890';
                                    let result = number.replace(/(d{4})(d{3})(d{3})/, "$1 $2 $3");
                                    console.warn(result);





                                    share|improve this answer


























                                      0












                                      0








                                      0






                                      You can do with this way.



                                      let number = '1234567890';
                                      let result = number.replace(/(d{4})(d{3})(d{3})/, "$1 $2 $3");
                                      console.warn(result);





                                      share|improve this answer














                                      You can do with this way.



                                      let number = '1234567890';
                                      let result = number.replace(/(d{4})(d{3})(d{3})/, "$1 $2 $3");
                                      console.warn(result);






                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Nov 23 '18 at 11:13

























                                      answered Nov 23 '18 at 6:44









                                      tron

                                      4318




                                      4318






























                                          draft saved

                                          draft discarded




















































                                          Thanks for contributing an answer to Stack Overflow!


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


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





                                          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                          Please pay close attention to the following guidance:


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


                                          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%2fstackoverflow.com%2fquestions%2f53441627%2fcustom-regex-to-format-number%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

                                          What visual should I use to simply compare current year value vs last year in Power BI desktop

                                          How to ignore python UserWarning in pytest?

                                          Alexandru Averescu