Is it possible in javascript regex, to match string inside the string matched by my capture group?












-3















I have following string dfed operator 11 - 145. I am trying to match string operator 11 and inside this matched string, i am trying to match string 11. Currently I successfully matched operator 11 with regex ((O|o)perator(i|I)?s*)d+(?=s*(-|_)s*d+). As I am in javascript, I can not use lookbehinds.



Is my approach correct? Is there any way to accomplish this in regex? How can i match string 11 inside previously matched string operator 11?



Thank you










share|improve this question




















  • 3





    Enclose the first d+ in another group? (d+)?

    – CertainPerformance
    Nov 20 '18 at 8:13











  • If you use s.match(/.../g), see How do you access the matched groups in a JavaScript regular expression?

    – Wiktor Stribiżew
    Nov 20 '18 at 8:18













  • @CertainPerformance could you provide demo of what you mean? Thank you

    – Tornike Shavishvili
    Nov 20 '18 at 8:30











  • @PoulBak If OP uses match with g, OP won't have the access to captured values. Thus, OP must learn how to do it, hence the link is provided.

    – Wiktor Stribiżew
    Nov 20 '18 at 8:31






  • 1





    @TornikeShavishvili See stackoverflow.com/questions/432493/…

    – Ivar
    Nov 20 '18 at 9:03
















-3















I have following string dfed operator 11 - 145. I am trying to match string operator 11 and inside this matched string, i am trying to match string 11. Currently I successfully matched operator 11 with regex ((O|o)perator(i|I)?s*)d+(?=s*(-|_)s*d+). As I am in javascript, I can not use lookbehinds.



Is my approach correct? Is there any way to accomplish this in regex? How can i match string 11 inside previously matched string operator 11?



Thank you










share|improve this question




















  • 3





    Enclose the first d+ in another group? (d+)?

    – CertainPerformance
    Nov 20 '18 at 8:13











  • If you use s.match(/.../g), see How do you access the matched groups in a JavaScript regular expression?

    – Wiktor Stribiżew
    Nov 20 '18 at 8:18













  • @CertainPerformance could you provide demo of what you mean? Thank you

    – Tornike Shavishvili
    Nov 20 '18 at 8:30











  • @PoulBak If OP uses match with g, OP won't have the access to captured values. Thus, OP must learn how to do it, hence the link is provided.

    – Wiktor Stribiżew
    Nov 20 '18 at 8:31






  • 1





    @TornikeShavishvili See stackoverflow.com/questions/432493/…

    – Ivar
    Nov 20 '18 at 9:03














-3












-3








-3


1






I have following string dfed operator 11 - 145. I am trying to match string operator 11 and inside this matched string, i am trying to match string 11. Currently I successfully matched operator 11 with regex ((O|o)perator(i|I)?s*)d+(?=s*(-|_)s*d+). As I am in javascript, I can not use lookbehinds.



Is my approach correct? Is there any way to accomplish this in regex? How can i match string 11 inside previously matched string operator 11?



Thank you










share|improve this question
















I have following string dfed operator 11 - 145. I am trying to match string operator 11 and inside this matched string, i am trying to match string 11. Currently I successfully matched operator 11 with regex ((O|o)perator(i|I)?s*)d+(?=s*(-|_)s*d+). As I am in javascript, I can not use lookbehinds.



Is my approach correct? Is there any way to accomplish this in regex? How can i match string 11 inside previously matched string operator 11?



Thank you







javascript regex






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 9:16









Poul Bak

5,45831232




5,45831232










asked Nov 20 '18 at 8:12









Tornike ShavishviliTornike Shavishvili

61021022




61021022








  • 3





    Enclose the first d+ in another group? (d+)?

    – CertainPerformance
    Nov 20 '18 at 8:13











  • If you use s.match(/.../g), see How do you access the matched groups in a JavaScript regular expression?

    – Wiktor Stribiżew
    Nov 20 '18 at 8:18













  • @CertainPerformance could you provide demo of what you mean? Thank you

    – Tornike Shavishvili
    Nov 20 '18 at 8:30











  • @PoulBak If OP uses match with g, OP won't have the access to captured values. Thus, OP must learn how to do it, hence the link is provided.

    – Wiktor Stribiżew
    Nov 20 '18 at 8:31






  • 1





    @TornikeShavishvili See stackoverflow.com/questions/432493/…

    – Ivar
    Nov 20 '18 at 9:03














  • 3





    Enclose the first d+ in another group? (d+)?

    – CertainPerformance
    Nov 20 '18 at 8:13











  • If you use s.match(/.../g), see How do you access the matched groups in a JavaScript regular expression?

    – Wiktor Stribiżew
    Nov 20 '18 at 8:18













  • @CertainPerformance could you provide demo of what you mean? Thank you

    – Tornike Shavishvili
    Nov 20 '18 at 8:30











  • @PoulBak If OP uses match with g, OP won't have the access to captured values. Thus, OP must learn how to do it, hence the link is provided.

    – Wiktor Stribiżew
    Nov 20 '18 at 8:31






  • 1





    @TornikeShavishvili See stackoverflow.com/questions/432493/…

    – Ivar
    Nov 20 '18 at 9:03








3




3





Enclose the first d+ in another group? (d+)?

– CertainPerformance
Nov 20 '18 at 8:13





Enclose the first d+ in another group? (d+)?

– CertainPerformance
Nov 20 '18 at 8:13













If you use s.match(/.../g), see How do you access the matched groups in a JavaScript regular expression?

– Wiktor Stribiżew
Nov 20 '18 at 8:18







If you use s.match(/.../g), see How do you access the matched groups in a JavaScript regular expression?

– Wiktor Stribiżew
Nov 20 '18 at 8:18















@CertainPerformance could you provide demo of what you mean? Thank you

– Tornike Shavishvili
Nov 20 '18 at 8:30





@CertainPerformance could you provide demo of what you mean? Thank you

– Tornike Shavishvili
Nov 20 '18 at 8:30













@PoulBak If OP uses match with g, OP won't have the access to captured values. Thus, OP must learn how to do it, hence the link is provided.

– Wiktor Stribiżew
Nov 20 '18 at 8:31





@PoulBak If OP uses match with g, OP won't have the access to captured values. Thus, OP must learn how to do it, hence the link is provided.

– Wiktor Stribiżew
Nov 20 '18 at 8:31




1




1





@TornikeShavishvili See stackoverflow.com/questions/432493/…

– Ivar
Nov 20 '18 at 9:03





@TornikeShavishvili See stackoverflow.com/questions/432493/…

– Ivar
Nov 20 '18 at 9:03












2 Answers
2






active

oldest

votes


















0














You can modify your regex by creating a group for the first number in the matched string:






const str = 'dfed  operator  11 -  145';
const regex = /([O|o]perator)[i|I]?s*(d+)*[?=s*(-|_)s*d+]/;
const found = str.match(regex);

console.log(found);
console.log(found[1]); // <-- group for string
console.log(found[2]); // <-- group for number








share|improve this answer





















  • 2





    Thank you. Is there any way to accomplish this with regex? not to use mach twice?

    – Tornike Shavishvili
    Nov 20 '18 at 8:21











  • The match twice is just for the code example

    – Yosvel Quintero
    Nov 20 '18 at 8:22











  • How would you get the same result with one call of match?

    – Tornike Shavishvili
    Nov 20 '18 at 8:26











  • Check the updated answer.. And just for you to know i have not downvoted your question

    – Yosvel Quintero
    Nov 20 '18 at 8:35






  • 1





    Thank you for your response. I have upvoted your answer. Is there any way to mach string inside the substring matched by certain capturing group? Would this be correct approach?

    – Tornike Shavishvili
    Nov 20 '18 at 9:12



















0














You could use (mind the case insensitive flag in the demo):



operatorD+(d+)


See a demo on regex101.com.






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%2f53388715%2fis-it-possible-in-javascript-regex-to-match-string-inside-the-string-matched-by%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You can modify your regex by creating a group for the first number in the matched string:






    const str = 'dfed  operator  11 -  145';
    const regex = /([O|o]perator)[i|I]?s*(d+)*[?=s*(-|_)s*d+]/;
    const found = str.match(regex);

    console.log(found);
    console.log(found[1]); // <-- group for string
    console.log(found[2]); // <-- group for number








    share|improve this answer





















    • 2





      Thank you. Is there any way to accomplish this with regex? not to use mach twice?

      – Tornike Shavishvili
      Nov 20 '18 at 8:21











    • The match twice is just for the code example

      – Yosvel Quintero
      Nov 20 '18 at 8:22











    • How would you get the same result with one call of match?

      – Tornike Shavishvili
      Nov 20 '18 at 8:26











    • Check the updated answer.. And just for you to know i have not downvoted your question

      – Yosvel Quintero
      Nov 20 '18 at 8:35






    • 1





      Thank you for your response. I have upvoted your answer. Is there any way to mach string inside the substring matched by certain capturing group? Would this be correct approach?

      – Tornike Shavishvili
      Nov 20 '18 at 9:12
















    0














    You can modify your regex by creating a group for the first number in the matched string:






    const str = 'dfed  operator  11 -  145';
    const regex = /([O|o]perator)[i|I]?s*(d+)*[?=s*(-|_)s*d+]/;
    const found = str.match(regex);

    console.log(found);
    console.log(found[1]); // <-- group for string
    console.log(found[2]); // <-- group for number








    share|improve this answer





















    • 2





      Thank you. Is there any way to accomplish this with regex? not to use mach twice?

      – Tornike Shavishvili
      Nov 20 '18 at 8:21











    • The match twice is just for the code example

      – Yosvel Quintero
      Nov 20 '18 at 8:22











    • How would you get the same result with one call of match?

      – Tornike Shavishvili
      Nov 20 '18 at 8:26











    • Check the updated answer.. And just for you to know i have not downvoted your question

      – Yosvel Quintero
      Nov 20 '18 at 8:35






    • 1





      Thank you for your response. I have upvoted your answer. Is there any way to mach string inside the substring matched by certain capturing group? Would this be correct approach?

      – Tornike Shavishvili
      Nov 20 '18 at 9:12














    0












    0








    0







    You can modify your regex by creating a group for the first number in the matched string:






    const str = 'dfed  operator  11 -  145';
    const regex = /([O|o]perator)[i|I]?s*(d+)*[?=s*(-|_)s*d+]/;
    const found = str.match(regex);

    console.log(found);
    console.log(found[1]); // <-- group for string
    console.log(found[2]); // <-- group for number








    share|improve this answer















    You can modify your regex by creating a group for the first number in the matched string:






    const str = 'dfed  operator  11 -  145';
    const regex = /([O|o]perator)[i|I]?s*(d+)*[?=s*(-|_)s*d+]/;
    const found = str.match(regex);

    console.log(found);
    console.log(found[1]); // <-- group for string
    console.log(found[2]); // <-- group for number








    const str = 'dfed  operator  11 -  145';
    const regex = /([O|o]perator)[i|I]?s*(d+)*[?=s*(-|_)s*d+]/;
    const found = str.match(regex);

    console.log(found);
    console.log(found[1]); // <-- group for string
    console.log(found[2]); // <-- group for number





    const str = 'dfed  operator  11 -  145';
    const regex = /([O|o]perator)[i|I]?s*(d+)*[?=s*(-|_)s*d+]/;
    const found = str.match(regex);

    console.log(found);
    console.log(found[1]); // <-- group for string
    console.log(found[2]); // <-- group for number






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 20 '18 at 9:21

























    answered Nov 20 '18 at 8:19









    Yosvel QuinteroYosvel Quintero

    11k42329




    11k42329








    • 2





      Thank you. Is there any way to accomplish this with regex? not to use mach twice?

      – Tornike Shavishvili
      Nov 20 '18 at 8:21











    • The match twice is just for the code example

      – Yosvel Quintero
      Nov 20 '18 at 8:22











    • How would you get the same result with one call of match?

      – Tornike Shavishvili
      Nov 20 '18 at 8:26











    • Check the updated answer.. And just for you to know i have not downvoted your question

      – Yosvel Quintero
      Nov 20 '18 at 8:35






    • 1





      Thank you for your response. I have upvoted your answer. Is there any way to mach string inside the substring matched by certain capturing group? Would this be correct approach?

      – Tornike Shavishvili
      Nov 20 '18 at 9:12














    • 2





      Thank you. Is there any way to accomplish this with regex? not to use mach twice?

      – Tornike Shavishvili
      Nov 20 '18 at 8:21











    • The match twice is just for the code example

      – Yosvel Quintero
      Nov 20 '18 at 8:22











    • How would you get the same result with one call of match?

      – Tornike Shavishvili
      Nov 20 '18 at 8:26











    • Check the updated answer.. And just for you to know i have not downvoted your question

      – Yosvel Quintero
      Nov 20 '18 at 8:35






    • 1





      Thank you for your response. I have upvoted your answer. Is there any way to mach string inside the substring matched by certain capturing group? Would this be correct approach?

      – Tornike Shavishvili
      Nov 20 '18 at 9:12








    2




    2





    Thank you. Is there any way to accomplish this with regex? not to use mach twice?

    – Tornike Shavishvili
    Nov 20 '18 at 8:21





    Thank you. Is there any way to accomplish this with regex? not to use mach twice?

    – Tornike Shavishvili
    Nov 20 '18 at 8:21













    The match twice is just for the code example

    – Yosvel Quintero
    Nov 20 '18 at 8:22





    The match twice is just for the code example

    – Yosvel Quintero
    Nov 20 '18 at 8:22













    How would you get the same result with one call of match?

    – Tornike Shavishvili
    Nov 20 '18 at 8:26





    How would you get the same result with one call of match?

    – Tornike Shavishvili
    Nov 20 '18 at 8:26













    Check the updated answer.. And just for you to know i have not downvoted your question

    – Yosvel Quintero
    Nov 20 '18 at 8:35





    Check the updated answer.. And just for you to know i have not downvoted your question

    – Yosvel Quintero
    Nov 20 '18 at 8:35




    1




    1





    Thank you for your response. I have upvoted your answer. Is there any way to mach string inside the substring matched by certain capturing group? Would this be correct approach?

    – Tornike Shavishvili
    Nov 20 '18 at 9:12





    Thank you for your response. I have upvoted your answer. Is there any way to mach string inside the substring matched by certain capturing group? Would this be correct approach?

    – Tornike Shavishvili
    Nov 20 '18 at 9:12













    0














    You could use (mind the case insensitive flag in the demo):



    operatorD+(d+)


    See a demo on regex101.com.






    share|improve this answer




























      0














      You could use (mind the case insensitive flag in the demo):



      operatorD+(d+)


      See a demo on regex101.com.






      share|improve this answer


























        0












        0








        0







        You could use (mind the case insensitive flag in the demo):



        operatorD+(d+)


        See a demo on regex101.com.






        share|improve this answer













        You could use (mind the case insensitive flag in the demo):



        operatorD+(d+)


        See a demo on regex101.com.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 '18 at 8:14









        JanJan

        24.4k52348




        24.4k52348






























            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%2f53388715%2fis-it-possible-in-javascript-regex-to-match-string-inside-the-string-matched-by%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

            Npm cannot find a required file even through it is in the searched directory