How to match multiple occurrences of a substring





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







9















If I have an HTML string such as:



<div><p>£20<span class="abc" /><span class="def">56</span></p></div>


And I want the text:



20<span class="abc" /><span class="def">56


How do I define a regular expression to match the target sections multiple times. So far I have:



str.match(/d*<[^>]*>d*/)


But this will only return the first number section 20<span class="abc" />



I need this to be flexible to match multiple tag / numeric sections while trimming anything leading or trailing the first / last digit in the string.










share|improve this question































    9















    If I have an HTML string such as:



    <div><p>£20<span class="abc" /><span class="def">56</span></p></div>


    And I want the text:



    20<span class="abc" /><span class="def">56


    How do I define a regular expression to match the target sections multiple times. So far I have:



    str.match(/d*<[^>]*>d*/)


    But this will only return the first number section 20<span class="abc" />



    I need this to be flexible to match multiple tag / numeric sections while trimming anything leading or trailing the first / last digit in the string.










    share|improve this question



























      9












      9








      9


      1






      If I have an HTML string such as:



      <div><p>£20<span class="abc" /><span class="def">56</span></p></div>


      And I want the text:



      20<span class="abc" /><span class="def">56


      How do I define a regular expression to match the target sections multiple times. So far I have:



      str.match(/d*<[^>]*>d*/)


      But this will only return the first number section 20<span class="abc" />



      I need this to be flexible to match multiple tag / numeric sections while trimming anything leading or trailing the first / last digit in the string.










      share|improve this question
















      If I have an HTML string such as:



      <div><p>£20<span class="abc" /><span class="def">56</span></p></div>


      And I want the text:



      20<span class="abc" /><span class="def">56


      How do I define a regular expression to match the target sections multiple times. So far I have:



      str.match(/d*<[^>]*>d*/)


      But this will only return the first number section 20<span class="abc" />



      I need this to be flexible to match multiple tag / numeric sections while trimming anything leading or trailing the first / last digit in the string.







      regex






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 3 at 9:28









      Josh Habdas

      3,1492634




      3,1492634










      asked Aug 12 '11 at 14:09









      gb2dgb2d

      3,41264183




      3,41264183
























          3 Answers
          3






          active

          oldest

          votes


















          5














          To match multiple times use to need use the global option



          str.match(/your_expression_here/g)
          ^





          share|improve this answer





















          • 4





            This is not answering his question.

            – Tim Pietzcker
            Aug 12 '11 at 14:56











          • With 5 upvotes in 7 years Tim is probably right. I added the correct answer.

            – Josh Habdas
            Jan 3 at 9:23



















          1














          Simply adding /g and calling it done isn't enough. Matching a substring within a string multiple times is trivial once you're aware of reluctant quantifiers—explained here with the solution to your problem.



          Given the string:



          <div><p>£20<span class="abc" /><span class="def">56</span></p></div>


          You will arrive at the text you wanted using:



          d+.*>d+


          But given the same string repeated two times:



          <div><p>£20<span class="abc" /><span class="def">56</span></p></div><div><p>£20<span class="abc" /><span class="def">56</span></p></div>


          You will not find the target selection multiple times. You'll only find it once due to the greedy nature of .*. To make .* non-greedy, or reluctant, simply add a ? after the * and you will arrive at:



          d+.*?>d+


          Which will find both occurrences of the substring you asked for as proven here.






          share|improve this answer































            0














            Just allow the group to be repeated: (?:...)+ means "Match ... 1 or more times:



            str.match(/d+(?:<[^>]*>)+d+/)


            As per Alan Moore's suggestion, I've also changed the d* into d+, making the numbers required instead of optional.






            share|improve this answer


























            • While you're at it, change each d* to d+; that's got to be a mistake.

              – Alan Moore
              Aug 13 '11 at 2:38











            • @Alan: Sure, you're right. Thanks!

              – Tim Pietzcker
              Aug 13 '11 at 5:09












            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%2f7041308%2fhow-to-match-multiple-occurrences-of-a-substring%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









            5














            To match multiple times use to need use the global option



            str.match(/your_expression_here/g)
            ^





            share|improve this answer





















            • 4





              This is not answering his question.

              – Tim Pietzcker
              Aug 12 '11 at 14:56











            • With 5 upvotes in 7 years Tim is probably right. I added the correct answer.

              – Josh Habdas
              Jan 3 at 9:23
















            5














            To match multiple times use to need use the global option



            str.match(/your_expression_here/g)
            ^





            share|improve this answer





















            • 4





              This is not answering his question.

              – Tim Pietzcker
              Aug 12 '11 at 14:56











            • With 5 upvotes in 7 years Tim is probably right. I added the correct answer.

              – Josh Habdas
              Jan 3 at 9:23














            5












            5








            5







            To match multiple times use to need use the global option



            str.match(/your_expression_here/g)
            ^





            share|improve this answer















            To match multiple times use to need use the global option



            str.match(/your_expression_here/g)
            ^






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jun 29 '18 at 13:29









            Wiktor Stribiżew

            329k16149228




            329k16149228










            answered Aug 12 '11 at 14:23









            James KyburzJames Kyburz

            9,26512530




            9,26512530








            • 4





              This is not answering his question.

              – Tim Pietzcker
              Aug 12 '11 at 14:56











            • With 5 upvotes in 7 years Tim is probably right. I added the correct answer.

              – Josh Habdas
              Jan 3 at 9:23














            • 4





              This is not answering his question.

              – Tim Pietzcker
              Aug 12 '11 at 14:56











            • With 5 upvotes in 7 years Tim is probably right. I added the correct answer.

              – Josh Habdas
              Jan 3 at 9:23








            4




            4





            This is not answering his question.

            – Tim Pietzcker
            Aug 12 '11 at 14:56





            This is not answering his question.

            – Tim Pietzcker
            Aug 12 '11 at 14:56













            With 5 upvotes in 7 years Tim is probably right. I added the correct answer.

            – Josh Habdas
            Jan 3 at 9:23





            With 5 upvotes in 7 years Tim is probably right. I added the correct answer.

            – Josh Habdas
            Jan 3 at 9:23













            1














            Simply adding /g and calling it done isn't enough. Matching a substring within a string multiple times is trivial once you're aware of reluctant quantifiers—explained here with the solution to your problem.



            Given the string:



            <div><p>£20<span class="abc" /><span class="def">56</span></p></div>


            You will arrive at the text you wanted using:



            d+.*>d+


            But given the same string repeated two times:



            <div><p>£20<span class="abc" /><span class="def">56</span></p></div><div><p>£20<span class="abc" /><span class="def">56</span></p></div>


            You will not find the target selection multiple times. You'll only find it once due to the greedy nature of .*. To make .* non-greedy, or reluctant, simply add a ? after the * and you will arrive at:



            d+.*?>d+


            Which will find both occurrences of the substring you asked for as proven here.






            share|improve this answer




























              1














              Simply adding /g and calling it done isn't enough. Matching a substring within a string multiple times is trivial once you're aware of reluctant quantifiers—explained here with the solution to your problem.



              Given the string:



              <div><p>£20<span class="abc" /><span class="def">56</span></p></div>


              You will arrive at the text you wanted using:



              d+.*>d+


              But given the same string repeated two times:



              <div><p>£20<span class="abc" /><span class="def">56</span></p></div><div><p>£20<span class="abc" /><span class="def">56</span></p></div>


              You will not find the target selection multiple times. You'll only find it once due to the greedy nature of .*. To make .* non-greedy, or reluctant, simply add a ? after the * and you will arrive at:



              d+.*?>d+


              Which will find both occurrences of the substring you asked for as proven here.






              share|improve this answer


























                1












                1








                1







                Simply adding /g and calling it done isn't enough. Matching a substring within a string multiple times is trivial once you're aware of reluctant quantifiers—explained here with the solution to your problem.



                Given the string:



                <div><p>£20<span class="abc" /><span class="def">56</span></p></div>


                You will arrive at the text you wanted using:



                d+.*>d+


                But given the same string repeated two times:



                <div><p>£20<span class="abc" /><span class="def">56</span></p></div><div><p>£20<span class="abc" /><span class="def">56</span></p></div>


                You will not find the target selection multiple times. You'll only find it once due to the greedy nature of .*. To make .* non-greedy, or reluctant, simply add a ? after the * and you will arrive at:



                d+.*?>d+


                Which will find both occurrences of the substring you asked for as proven here.






                share|improve this answer













                Simply adding /g and calling it done isn't enough. Matching a substring within a string multiple times is trivial once you're aware of reluctant quantifiers—explained here with the solution to your problem.



                Given the string:



                <div><p>£20<span class="abc" /><span class="def">56</span></p></div>


                You will arrive at the text you wanted using:



                d+.*>d+


                But given the same string repeated two times:



                <div><p>£20<span class="abc" /><span class="def">56</span></p></div><div><p>£20<span class="abc" /><span class="def">56</span></p></div>


                You will not find the target selection multiple times. You'll only find it once due to the greedy nature of .*. To make .* non-greedy, or reluctant, simply add a ? after the * and you will arrive at:



                d+.*?>d+


                Which will find both occurrences of the substring you asked for as proven here.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 3 at 9:21









                Josh HabdasJosh Habdas

                3,1492634




                3,1492634























                    0














                    Just allow the group to be repeated: (?:...)+ means "Match ... 1 or more times:



                    str.match(/d+(?:<[^>]*>)+d+/)


                    As per Alan Moore's suggestion, I've also changed the d* into d+, making the numbers required instead of optional.






                    share|improve this answer


























                    • While you're at it, change each d* to d+; that's got to be a mistake.

                      – Alan Moore
                      Aug 13 '11 at 2:38











                    • @Alan: Sure, you're right. Thanks!

                      – Tim Pietzcker
                      Aug 13 '11 at 5:09
















                    0














                    Just allow the group to be repeated: (?:...)+ means "Match ... 1 or more times:



                    str.match(/d+(?:<[^>]*>)+d+/)


                    As per Alan Moore's suggestion, I've also changed the d* into d+, making the numbers required instead of optional.






                    share|improve this answer


























                    • While you're at it, change each d* to d+; that's got to be a mistake.

                      – Alan Moore
                      Aug 13 '11 at 2:38











                    • @Alan: Sure, you're right. Thanks!

                      – Tim Pietzcker
                      Aug 13 '11 at 5:09














                    0












                    0








                    0







                    Just allow the group to be repeated: (?:...)+ means "Match ... 1 or more times:



                    str.match(/d+(?:<[^>]*>)+d+/)


                    As per Alan Moore's suggestion, I've also changed the d* into d+, making the numbers required instead of optional.






                    share|improve this answer















                    Just allow the group to be repeated: (?:...)+ means "Match ... 1 or more times:



                    str.match(/d+(?:<[^>]*>)+d+/)


                    As per Alan Moore's suggestion, I've also changed the d* into d+, making the numbers required instead of optional.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Aug 13 '11 at 5:09

























                    answered Aug 12 '11 at 14:56









                    Tim PietzckerTim Pietzcker

                    252k43381462




                    252k43381462













                    • While you're at it, change each d* to d+; that's got to be a mistake.

                      – Alan Moore
                      Aug 13 '11 at 2:38











                    • @Alan: Sure, you're right. Thanks!

                      – Tim Pietzcker
                      Aug 13 '11 at 5:09



















                    • While you're at it, change each d* to d+; that's got to be a mistake.

                      – Alan Moore
                      Aug 13 '11 at 2:38











                    • @Alan: Sure, you're right. Thanks!

                      – Tim Pietzcker
                      Aug 13 '11 at 5:09

















                    While you're at it, change each d* to d+; that's got to be a mistake.

                    – Alan Moore
                    Aug 13 '11 at 2:38





                    While you're at it, change each d* to d+; that's got to be a mistake.

                    – Alan Moore
                    Aug 13 '11 at 2:38













                    @Alan: Sure, you're right. Thanks!

                    – Tim Pietzcker
                    Aug 13 '11 at 5:09





                    @Alan: Sure, you're right. Thanks!

                    – Tim Pietzcker
                    Aug 13 '11 at 5:09


















                    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%2f7041308%2fhow-to-match-multiple-occurrences-of-a-substring%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

                    MongoDB - Not Authorized To Execute Command

                    in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

                    How to fix TextFormField cause rebuild widget in Flutter