RegExp test function odd behavior





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I'm checking whether character is capital letter or not by using javascript RegExp



function splitWords(text) {
const capReg = /[A-Z]/g;
const alphaNumReg = /[a-z0-9]/g;

for (let i = 0; i <= text.length - 1; i++) {
console.log(
text[i], text[i + 1], text[i + 2],
capReg.test(text[i]), capReg.test(text[i + 1]),
alphaNumReg.test(text[i + 2])
);
}
}
splitWords('ABCOption');


at case expected C, O, p, true, true, true
Actual C, O, p, true, false, true



Please help me where i'm doing wrong










share|improve this question























  • i cannot explain it well but i know that the behavior of test() is not as expected. I use developer.mozilla.org/de/docs/Web/JavaScript/Reference/… for testing for occurances. similar, but you ask the string for to match the regexp and not the aother way around.

    – Paulquappe
    Jan 4 at 13:24


















0















I'm checking whether character is capital letter or not by using javascript RegExp



function splitWords(text) {
const capReg = /[A-Z]/g;
const alphaNumReg = /[a-z0-9]/g;

for (let i = 0; i <= text.length - 1; i++) {
console.log(
text[i], text[i + 1], text[i + 2],
capReg.test(text[i]), capReg.test(text[i + 1]),
alphaNumReg.test(text[i + 2])
);
}
}
splitWords('ABCOption');


at case expected C, O, p, true, true, true
Actual C, O, p, true, false, true



Please help me where i'm doing wrong










share|improve this question























  • i cannot explain it well but i know that the behavior of test() is not as expected. I use developer.mozilla.org/de/docs/Web/JavaScript/Reference/… for testing for occurances. similar, but you ask the string for to match the regexp and not the aother way around.

    – Paulquappe
    Jan 4 at 13:24














0












0








0








I'm checking whether character is capital letter or not by using javascript RegExp



function splitWords(text) {
const capReg = /[A-Z]/g;
const alphaNumReg = /[a-z0-9]/g;

for (let i = 0; i <= text.length - 1; i++) {
console.log(
text[i], text[i + 1], text[i + 2],
capReg.test(text[i]), capReg.test(text[i + 1]),
alphaNumReg.test(text[i + 2])
);
}
}
splitWords('ABCOption');


at case expected C, O, p, true, true, true
Actual C, O, p, true, false, true



Please help me where i'm doing wrong










share|improve this question














I'm checking whether character is capital letter or not by using javascript RegExp



function splitWords(text) {
const capReg = /[A-Z]/g;
const alphaNumReg = /[a-z0-9]/g;

for (let i = 0; i <= text.length - 1; i++) {
console.log(
text[i], text[i + 1], text[i + 2],
capReg.test(text[i]), capReg.test(text[i + 1]),
alphaNumReg.test(text[i + 2])
);
}
}
splitWords('ABCOption');


at case expected C, O, p, true, true, true
Actual C, O, p, true, false, true



Please help me where i'm doing wrong







javascript






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 4 at 13:16









Yaswanth GoliYaswanth Goli

31




31













  • i cannot explain it well but i know that the behavior of test() is not as expected. I use developer.mozilla.org/de/docs/Web/JavaScript/Reference/… for testing for occurances. similar, but you ask the string for to match the regexp and not the aother way around.

    – Paulquappe
    Jan 4 at 13:24



















  • i cannot explain it well but i know that the behavior of test() is not as expected. I use developer.mozilla.org/de/docs/Web/JavaScript/Reference/… for testing for occurances. similar, but you ask the string for to match the regexp and not the aother way around.

    – Paulquappe
    Jan 4 at 13:24

















i cannot explain it well but i know that the behavior of test() is not as expected. I use developer.mozilla.org/de/docs/Web/JavaScript/Reference/… for testing for occurances. similar, but you ask the string for to match the regexp and not the aother way around.

– Paulquappe
Jan 4 at 13:24





i cannot explain it well but i know that the behavior of test() is not as expected. I use developer.mozilla.org/de/docs/Web/JavaScript/Reference/… for testing for occurances. similar, but you ask the string for to match the regexp and not the aother way around.

– Paulquappe
Jan 4 at 13:24












3 Answers
3






active

oldest

votes


















0














You don't need the g part in your regex if you're checking character by character; g is used when you don't want to stop at the first match. Just replace your regex by /[A-Z]/ and it will work as expected.



Moreover, if you want to split a string into words based on uppercased letters, you can do it directly with patterns. Check this SO question to see some solutions






share|improve this answer































    1














    This is how you can get array and check every capital letter:






    const res = Array.from("ABCOption").map(e=>/[A-Z]/.test(e));

    console.log(res)








    share|improve this answer
























    • +1 for this great solution, but it would be stronger if you use const res = Array.from(text).map(e => e === e.toUpperCase());, so it will also recognize foreign upper case letters, like Ñ. Cannot imagine why OP accepts the other answer though.

      – evayly
      Jan 4 at 13:38











    • @evayly If you're refering to the previous selected answer, my guess is that the code was the same as his own (except for the working regex) and/or the OP does not know ES6 syntax and couldn't figure out how to implement his function inside ES6 arrow functions. If you're talking about mine, it contains a working regex and a link to implement a split function in a cleaner way than what he was going to do.

      – Carrm
      Jan 7 at 16:09



















    1














    the below code worked for me hope work for you also. you just have to change your regex like below



    function splitWords(text) {
    const capReg = /^[A-Z]*$/;// /[A-Z]/g just replace your regexp and try ;
    const alphaNumReg = /^[a-z0-9]*$/;// /[a-z0-9]/g ;

    for (let i = 0; i <= text.length - 1; i++) {
    console.log(
    text[i], text[i + 1], text[i + 2],
    capReg.test(text[i]), capReg.test(text[i + 1]),
    alphaNumReg.test(text[i + 2])
    );
    }
    }





    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%2f54039689%2fregexp-test-function-odd-behavior%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









      0














      You don't need the g part in your regex if you're checking character by character; g is used when you don't want to stop at the first match. Just replace your regex by /[A-Z]/ and it will work as expected.



      Moreover, if you want to split a string into words based on uppercased letters, you can do it directly with patterns. Check this SO question to see some solutions






      share|improve this answer




























        0














        You don't need the g part in your regex if you're checking character by character; g is used when you don't want to stop at the first match. Just replace your regex by /[A-Z]/ and it will work as expected.



        Moreover, if you want to split a string into words based on uppercased letters, you can do it directly with patterns. Check this SO question to see some solutions






        share|improve this answer


























          0












          0








          0







          You don't need the g part in your regex if you're checking character by character; g is used when you don't want to stop at the first match. Just replace your regex by /[A-Z]/ and it will work as expected.



          Moreover, if you want to split a string into words based on uppercased letters, you can do it directly with patterns. Check this SO question to see some solutions






          share|improve this answer













          You don't need the g part in your regex if you're checking character by character; g is used when you don't want to stop at the first match. Just replace your regex by /[A-Z]/ and it will work as expected.



          Moreover, if you want to split a string into words based on uppercased letters, you can do it directly with patterns. Check this SO question to see some solutions







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 4 at 13:44









          CarrmCarrm

          61121328




          61121328

























              1














              This is how you can get array and check every capital letter:






              const res = Array.from("ABCOption").map(e=>/[A-Z]/.test(e));

              console.log(res)








              share|improve this answer
























              • +1 for this great solution, but it would be stronger if you use const res = Array.from(text).map(e => e === e.toUpperCase());, so it will also recognize foreign upper case letters, like Ñ. Cannot imagine why OP accepts the other answer though.

                – evayly
                Jan 4 at 13:38











              • @evayly If you're refering to the previous selected answer, my guess is that the code was the same as his own (except for the working regex) and/or the OP does not know ES6 syntax and couldn't figure out how to implement his function inside ES6 arrow functions. If you're talking about mine, it contains a working regex and a link to implement a split function in a cleaner way than what he was going to do.

                – Carrm
                Jan 7 at 16:09
















              1














              This is how you can get array and check every capital letter:






              const res = Array.from("ABCOption").map(e=>/[A-Z]/.test(e));

              console.log(res)








              share|improve this answer
























              • +1 for this great solution, but it would be stronger if you use const res = Array.from(text).map(e => e === e.toUpperCase());, so it will also recognize foreign upper case letters, like Ñ. Cannot imagine why OP accepts the other answer though.

                – evayly
                Jan 4 at 13:38











              • @evayly If you're refering to the previous selected answer, my guess is that the code was the same as his own (except for the working regex) and/or the OP does not know ES6 syntax and couldn't figure out how to implement his function inside ES6 arrow functions. If you're talking about mine, it contains a working regex and a link to implement a split function in a cleaner way than what he was going to do.

                – Carrm
                Jan 7 at 16:09














              1












              1








              1







              This is how you can get array and check every capital letter:






              const res = Array.from("ABCOption").map(e=>/[A-Z]/.test(e));

              console.log(res)








              share|improve this answer













              This is how you can get array and check every capital letter:






              const res = Array.from("ABCOption").map(e=>/[A-Z]/.test(e));

              console.log(res)








              const res = Array.from("ABCOption").map(e=>/[A-Z]/.test(e));

              console.log(res)





              const res = Array.from("ABCOption").map(e=>/[A-Z]/.test(e));

              console.log(res)






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Jan 4 at 13:25









              Vadim HulevichVadim Hulevich

              846212




              846212













              • +1 for this great solution, but it would be stronger if you use const res = Array.from(text).map(e => e === e.toUpperCase());, so it will also recognize foreign upper case letters, like Ñ. Cannot imagine why OP accepts the other answer though.

                – evayly
                Jan 4 at 13:38











              • @evayly If you're refering to the previous selected answer, my guess is that the code was the same as his own (except for the working regex) and/or the OP does not know ES6 syntax and couldn't figure out how to implement his function inside ES6 arrow functions. If you're talking about mine, it contains a working regex and a link to implement a split function in a cleaner way than what he was going to do.

                – Carrm
                Jan 7 at 16:09



















              • +1 for this great solution, but it would be stronger if you use const res = Array.from(text).map(e => e === e.toUpperCase());, so it will also recognize foreign upper case letters, like Ñ. Cannot imagine why OP accepts the other answer though.

                – evayly
                Jan 4 at 13:38











              • @evayly If you're refering to the previous selected answer, my guess is that the code was the same as his own (except for the working regex) and/or the OP does not know ES6 syntax and couldn't figure out how to implement his function inside ES6 arrow functions. If you're talking about mine, it contains a working regex and a link to implement a split function in a cleaner way than what he was going to do.

                – Carrm
                Jan 7 at 16:09

















              +1 for this great solution, but it would be stronger if you use const res = Array.from(text).map(e => e === e.toUpperCase());, so it will also recognize foreign upper case letters, like Ñ. Cannot imagine why OP accepts the other answer though.

              – evayly
              Jan 4 at 13:38





              +1 for this great solution, but it would be stronger if you use const res = Array.from(text).map(e => e === e.toUpperCase());, so it will also recognize foreign upper case letters, like Ñ. Cannot imagine why OP accepts the other answer though.

              – evayly
              Jan 4 at 13:38













              @evayly If you're refering to the previous selected answer, my guess is that the code was the same as his own (except for the working regex) and/or the OP does not know ES6 syntax and couldn't figure out how to implement his function inside ES6 arrow functions. If you're talking about mine, it contains a working regex and a link to implement a split function in a cleaner way than what he was going to do.

              – Carrm
              Jan 7 at 16:09





              @evayly If you're refering to the previous selected answer, my guess is that the code was the same as his own (except for the working regex) and/or the OP does not know ES6 syntax and couldn't figure out how to implement his function inside ES6 arrow functions. If you're talking about mine, it contains a working regex and a link to implement a split function in a cleaner way than what he was going to do.

              – Carrm
              Jan 7 at 16:09











              1














              the below code worked for me hope work for you also. you just have to change your regex like below



              function splitWords(text) {
              const capReg = /^[A-Z]*$/;// /[A-Z]/g just replace your regexp and try ;
              const alphaNumReg = /^[a-z0-9]*$/;// /[a-z0-9]/g ;

              for (let i = 0; i <= text.length - 1; i++) {
              console.log(
              text[i], text[i + 1], text[i + 2],
              capReg.test(text[i]), capReg.test(text[i + 1]),
              alphaNumReg.test(text[i + 2])
              );
              }
              }





              share|improve this answer




























                1














                the below code worked for me hope work for you also. you just have to change your regex like below



                function splitWords(text) {
                const capReg = /^[A-Z]*$/;// /[A-Z]/g just replace your regexp and try ;
                const alphaNumReg = /^[a-z0-9]*$/;// /[a-z0-9]/g ;

                for (let i = 0; i <= text.length - 1; i++) {
                console.log(
                text[i], text[i + 1], text[i + 2],
                capReg.test(text[i]), capReg.test(text[i + 1]),
                alphaNumReg.test(text[i + 2])
                );
                }
                }





                share|improve this answer


























                  1












                  1








                  1







                  the below code worked for me hope work for you also. you just have to change your regex like below



                  function splitWords(text) {
                  const capReg = /^[A-Z]*$/;// /[A-Z]/g just replace your regexp and try ;
                  const alphaNumReg = /^[a-z0-9]*$/;// /[a-z0-9]/g ;

                  for (let i = 0; i <= text.length - 1; i++) {
                  console.log(
                  text[i], text[i + 1], text[i + 2],
                  capReg.test(text[i]), capReg.test(text[i + 1]),
                  alphaNumReg.test(text[i + 2])
                  );
                  }
                  }





                  share|improve this answer













                  the below code worked for me hope work for you also. you just have to change your regex like below



                  function splitWords(text) {
                  const capReg = /^[A-Z]*$/;// /[A-Z]/g just replace your regexp and try ;
                  const alphaNumReg = /^[a-z0-9]*$/;// /[a-z0-9]/g ;

                  for (let i = 0; i <= text.length - 1; i++) {
                  console.log(
                  text[i], text[i + 1], text[i + 2],
                  capReg.test(text[i]), capReg.test(text[i + 1]),
                  alphaNumReg.test(text[i + 2])
                  );
                  }
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 4 at 13:33









                  jasmin ravaljasmin raval

                  7210




                  7210






























                      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.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54039689%2fregexp-test-function-odd-behavior%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

                      Mossoró

                      Error while reading .h5 file using the rhdf5 package in R

                      Pushsharp Apns notification error: 'InvalidToken'