getting numbers from a string with the help of javascript











up vote
1
down vote

favorite












I am working in a project which it needs to get a phone number from a string. the string is like this in Java Script array named dine



{
"group": 1,

"tel1": "Tél(1): 05.82.77.31.78",
"tel2": "Tél(2): 09.55.86.31.45",

},
,...


I want to use pure phone numbers in a tag like this



<a href="tel:0970500469">Tél: 09.70.50.04.69</a>


I have used this java script codes



for (var i = 0; i < dine.length; i++) {
telnom = dine[i].tel1;
telnom = telnom.replace(/D/g, '');

alert(telnom);

telnom2 = dine[i].tel2;
telnom2 = telnom2.replace(/D/g, '');
alert(telnom2);

infoHtml += '<div class="info">n <p> <a href="tel:' + telnom + '">' + dine[i].tel1 + '</a></p>n <p> <a href="tel:' + telnom2 + '">' + dine[i].tel2 + '</a></p>n n</div>';
}


the problem is that it added 1 at the beginning of my phone number that it seems belongs to the Tél(1) and makes the telnom1 like this : 10970500469 while I want to call 0970500469



I am sure the problem stand for the way I used replace codes but I really do not know how to correct it.



 telnom = dine[i].tel1;

telnom = telnom.replace(/D/g, '');

telnom2 = dine[i].tel2;

telnom2 = telnom2.replace(/D/g, '');


I really appreciate any help. thank you










share|improve this question




























    up vote
    1
    down vote

    favorite












    I am working in a project which it needs to get a phone number from a string. the string is like this in Java Script array named dine



    {
    "group": 1,

    "tel1": "Tél(1): 05.82.77.31.78",
    "tel2": "Tél(2): 09.55.86.31.45",

    },
    ,...


    I want to use pure phone numbers in a tag like this



    <a href="tel:0970500469">Tél: 09.70.50.04.69</a>


    I have used this java script codes



    for (var i = 0; i < dine.length; i++) {
    telnom = dine[i].tel1;
    telnom = telnom.replace(/D/g, '');

    alert(telnom);

    telnom2 = dine[i].tel2;
    telnom2 = telnom2.replace(/D/g, '');
    alert(telnom2);

    infoHtml += '<div class="info">n <p> <a href="tel:' + telnom + '">' + dine[i].tel1 + '</a></p>n <p> <a href="tel:' + telnom2 + '">' + dine[i].tel2 + '</a></p>n n</div>';
    }


    the problem is that it added 1 at the beginning of my phone number that it seems belongs to the Tél(1) and makes the telnom1 like this : 10970500469 while I want to call 0970500469



    I am sure the problem stand for the way I used replace codes but I really do not know how to correct it.



     telnom = dine[i].tel1;

    telnom = telnom.replace(/D/g, '');

    telnom2 = dine[i].tel2;

    telnom2 = telnom2.replace(/D/g, '');


    I really appreciate any help. thank you










    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I am working in a project which it needs to get a phone number from a string. the string is like this in Java Script array named dine



      {
      "group": 1,

      "tel1": "Tél(1): 05.82.77.31.78",
      "tel2": "Tél(2): 09.55.86.31.45",

      },
      ,...


      I want to use pure phone numbers in a tag like this



      <a href="tel:0970500469">Tél: 09.70.50.04.69</a>


      I have used this java script codes



      for (var i = 0; i < dine.length; i++) {
      telnom = dine[i].tel1;
      telnom = telnom.replace(/D/g, '');

      alert(telnom);

      telnom2 = dine[i].tel2;
      telnom2 = telnom2.replace(/D/g, '');
      alert(telnom2);

      infoHtml += '<div class="info">n <p> <a href="tel:' + telnom + '">' + dine[i].tel1 + '</a></p>n <p> <a href="tel:' + telnom2 + '">' + dine[i].tel2 + '</a></p>n n</div>';
      }


      the problem is that it added 1 at the beginning of my phone number that it seems belongs to the Tél(1) and makes the telnom1 like this : 10970500469 while I want to call 0970500469



      I am sure the problem stand for the way I used replace codes but I really do not know how to correct it.



       telnom = dine[i].tel1;

      telnom = telnom.replace(/D/g, '');

      telnom2 = dine[i].tel2;

      telnom2 = telnom2.replace(/D/g, '');


      I really appreciate any help. thank you










      share|improve this question















      I am working in a project which it needs to get a phone number from a string. the string is like this in Java Script array named dine



      {
      "group": 1,

      "tel1": "Tél(1): 05.82.77.31.78",
      "tel2": "Tél(2): 09.55.86.31.45",

      },
      ,...


      I want to use pure phone numbers in a tag like this



      <a href="tel:0970500469">Tél: 09.70.50.04.69</a>


      I have used this java script codes



      for (var i = 0; i < dine.length; i++) {
      telnom = dine[i].tel1;
      telnom = telnom.replace(/D/g, '');

      alert(telnom);

      telnom2 = dine[i].tel2;
      telnom2 = telnom2.replace(/D/g, '');
      alert(telnom2);

      infoHtml += '<div class="info">n <p> <a href="tel:' + telnom + '">' + dine[i].tel1 + '</a></p>n <p> <a href="tel:' + telnom2 + '">' + dine[i].tel2 + '</a></p>n n</div>';
      }


      the problem is that it added 1 at the beginning of my phone number that it seems belongs to the Tél(1) and makes the telnom1 like this : 10970500469 while I want to call 0970500469



      I am sure the problem stand for the way I used replace codes but I really do not know how to correct it.



       telnom = dine[i].tel1;

      telnom = telnom.replace(/D/g, '');

      telnom2 = dine[i].tel2;

      telnom2 = telnom2.replace(/D/g, '');


      I really appreciate any help. thank you







      javascript jquery arrays






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 at 11:33









      adiga

      5,30461838




      5,30461838










      asked Nov 22 at 11:27









      neda Derakhshesh

      499320




      499320
























          3 Answers
          3






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          You need to remove any digits that appear within brackets, as well as non digits. Your regext should be /((d*)|D)/g






          var telnom = "Tel:(1) - 05.34.36.15"

          telnom = telnom.replace(/((d*)|D)/g, '');

          console.log(telnom); //05343615








          share|improve this answer





















          • it seems it works fine. thank you . may I ask you a brief explanation ? you remove every characters plus anything in bracket right? good job thank you
            – neda Derakhshesh
            Nov 22 at 12:37






          • 1




            Yes exactly. @nedaDerakhshesh
            – Ahmad
            Nov 22 at 14:46


















          up vote
          1
          down vote













          If your phone numbers always start with something like Tel:, Tel(1): etc you could, for example, consider simply splitting the whole string first using : (or a space) as separators. Like this:



          telnom = dine[i].tel1;
          telnom = telnom.split(":")[1].replace(/D/g, '');





          share|improve this answer




























            up vote
            1
            down vote













            You can use the following regex to get the desired telephone number:






            var n = "Tél(1): 05.82.77.31.78".match(/[d]d+/g).join("");
            console.log(n);





            Explanation: [d] matches only numbers and we are adding another d so only two numbers which are side-by-side matches. Next, we join them to a single string. If you need number instead of the string you can do following:
            Number(n)






            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',
              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%2f53429960%2fgetting-numbers-from-a-string-with-the-help-of-javascript%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              2
              down vote



              accepted










              You need to remove any digits that appear within brackets, as well as non digits. Your regext should be /((d*)|D)/g






              var telnom = "Tel:(1) - 05.34.36.15"

              telnom = telnom.replace(/((d*)|D)/g, '');

              console.log(telnom); //05343615








              share|improve this answer





















              • it seems it works fine. thank you . may I ask you a brief explanation ? you remove every characters plus anything in bracket right? good job thank you
                – neda Derakhshesh
                Nov 22 at 12:37






              • 1




                Yes exactly. @nedaDerakhshesh
                – Ahmad
                Nov 22 at 14:46















              up vote
              2
              down vote



              accepted










              You need to remove any digits that appear within brackets, as well as non digits. Your regext should be /((d*)|D)/g






              var telnom = "Tel:(1) - 05.34.36.15"

              telnom = telnom.replace(/((d*)|D)/g, '');

              console.log(telnom); //05343615








              share|improve this answer





















              • it seems it works fine. thank you . may I ask you a brief explanation ? you remove every characters plus anything in bracket right? good job thank you
                – neda Derakhshesh
                Nov 22 at 12:37






              • 1




                Yes exactly. @nedaDerakhshesh
                – Ahmad
                Nov 22 at 14:46













              up vote
              2
              down vote



              accepted







              up vote
              2
              down vote



              accepted






              You need to remove any digits that appear within brackets, as well as non digits. Your regext should be /((d*)|D)/g






              var telnom = "Tel:(1) - 05.34.36.15"

              telnom = telnom.replace(/((d*)|D)/g, '');

              console.log(telnom); //05343615








              share|improve this answer












              You need to remove any digits that appear within brackets, as well as non digits. Your regext should be /((d*)|D)/g






              var telnom = "Tel:(1) - 05.34.36.15"

              telnom = telnom.replace(/((d*)|D)/g, '');

              console.log(telnom); //05343615








              var telnom = "Tel:(1) - 05.34.36.15"

              telnom = telnom.replace(/((d*)|D)/g, '');

              console.log(telnom); //05343615





              var telnom = "Tel:(1) - 05.34.36.15"

              telnom = telnom.replace(/((d*)|D)/g, '');

              console.log(telnom); //05343615






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 22 at 11:37









              Ahmad

              7,79143463




              7,79143463












              • it seems it works fine. thank you . may I ask you a brief explanation ? you remove every characters plus anything in bracket right? good job thank you
                – neda Derakhshesh
                Nov 22 at 12:37






              • 1




                Yes exactly. @nedaDerakhshesh
                – Ahmad
                Nov 22 at 14:46


















              • it seems it works fine. thank you . may I ask you a brief explanation ? you remove every characters plus anything in bracket right? good job thank you
                – neda Derakhshesh
                Nov 22 at 12:37






              • 1




                Yes exactly. @nedaDerakhshesh
                – Ahmad
                Nov 22 at 14:46
















              it seems it works fine. thank you . may I ask you a brief explanation ? you remove every characters plus anything in bracket right? good job thank you
              – neda Derakhshesh
              Nov 22 at 12:37




              it seems it works fine. thank you . may I ask you a brief explanation ? you remove every characters plus anything in bracket right? good job thank you
              – neda Derakhshesh
              Nov 22 at 12:37




              1




              1




              Yes exactly. @nedaDerakhshesh
              – Ahmad
              Nov 22 at 14:46




              Yes exactly. @nedaDerakhshesh
              – Ahmad
              Nov 22 at 14:46












              up vote
              1
              down vote













              If your phone numbers always start with something like Tel:, Tel(1): etc you could, for example, consider simply splitting the whole string first using : (or a space) as separators. Like this:



              telnom = dine[i].tel1;
              telnom = telnom.split(":")[1].replace(/D/g, '');





              share|improve this answer

























                up vote
                1
                down vote













                If your phone numbers always start with something like Tel:, Tel(1): etc you could, for example, consider simply splitting the whole string first using : (or a space) as separators. Like this:



                telnom = dine[i].tel1;
                telnom = telnom.split(":")[1].replace(/D/g, '');





                share|improve this answer























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  If your phone numbers always start with something like Tel:, Tel(1): etc you could, for example, consider simply splitting the whole string first using : (or a space) as separators. Like this:



                  telnom = dine[i].tel1;
                  telnom = telnom.split(":")[1].replace(/D/g, '');





                  share|improve this answer












                  If your phone numbers always start with something like Tel:, Tel(1): etc you could, for example, consider simply splitting the whole string first using : (or a space) as separators. Like this:



                  telnom = dine[i].tel1;
                  telnom = telnom.split(":")[1].replace(/D/g, '');






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 22 at 11:39









                  Szab

                  758415




                  758415






















                      up vote
                      1
                      down vote













                      You can use the following regex to get the desired telephone number:






                      var n = "Tél(1): 05.82.77.31.78".match(/[d]d+/g).join("");
                      console.log(n);





                      Explanation: [d] matches only numbers and we are adding another d so only two numbers which are side-by-side matches. Next, we join them to a single string. If you need number instead of the string you can do following:
                      Number(n)






                      share|improve this answer



























                        up vote
                        1
                        down vote













                        You can use the following regex to get the desired telephone number:






                        var n = "Tél(1): 05.82.77.31.78".match(/[d]d+/g).join("");
                        console.log(n);





                        Explanation: [d] matches only numbers and we are adding another d so only two numbers which are side-by-side matches. Next, we join them to a single string. If you need number instead of the string you can do following:
                        Number(n)






                        share|improve this answer

























                          up vote
                          1
                          down vote










                          up vote
                          1
                          down vote









                          You can use the following regex to get the desired telephone number:






                          var n = "Tél(1): 05.82.77.31.78".match(/[d]d+/g).join("");
                          console.log(n);





                          Explanation: [d] matches only numbers and we are adding another d so only two numbers which are side-by-side matches. Next, we join them to a single string. If you need number instead of the string you can do following:
                          Number(n)






                          share|improve this answer














                          You can use the following regex to get the desired telephone number:






                          var n = "Tél(1): 05.82.77.31.78".match(/[d]d+/g).join("");
                          console.log(n);





                          Explanation: [d] matches only numbers and we are adding another d so only two numbers which are side-by-side matches. Next, we join them to a single string. If you need number instead of the string you can do following:
                          Number(n)






                          var n = "Tél(1): 05.82.77.31.78".match(/[d]d+/g).join("");
                          console.log(n);





                          var n = "Tél(1): 05.82.77.31.78".match(/[d]d+/g).join("");
                          console.log(n);






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 22 at 12:00

























                          answered Nov 22 at 11:52









                          im_tsm

                          701320




                          701320






























                              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%2f53429960%2fgetting-numbers-from-a-string-with-the-help-of-javascript%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

                              Alexandru Averescu

                              Trompette piccolo