Regex is not capturing value [duplicate]












1
















This question already has an answer here:




  • Alternative to deprecated RegExp.$n object properties

    1 answer




I have the following javascript code running on node.js version 8.9.4 :






if (/^PROBLEM_(d+)_YES_(d+)_$/.test('PROBLEM_5_YES_1_')) {
console.log("Start"); //Start
console.log(RegExp.$2); //
console.log(RegExp.$1); //
console.log("Printed"); //Printed
}





The problem is the regex test is returning true but RegExp.$1 and $2 all are being empty. Values are not being captured.
What am I doing wrong?










share|improve this question















marked as duplicate by Wiktor Stribiżew javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 8:51


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 1





    I guess - test doesn't capture group values in js. Try using exec

    – user2932057
    Nov 22 '18 at 6:49
















1
















This question already has an answer here:




  • Alternative to deprecated RegExp.$n object properties

    1 answer




I have the following javascript code running on node.js version 8.9.4 :






if (/^PROBLEM_(d+)_YES_(d+)_$/.test('PROBLEM_5_YES_1_')) {
console.log("Start"); //Start
console.log(RegExp.$2); //
console.log(RegExp.$1); //
console.log("Printed"); //Printed
}





The problem is the regex test is returning true but RegExp.$1 and $2 all are being empty. Values are not being captured.
What am I doing wrong?










share|improve this question















marked as duplicate by Wiktor Stribiżew javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 8:51


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 1





    I guess - test doesn't capture group values in js. Try using exec

    – user2932057
    Nov 22 '18 at 6:49














1












1








1









This question already has an answer here:




  • Alternative to deprecated RegExp.$n object properties

    1 answer




I have the following javascript code running on node.js version 8.9.4 :






if (/^PROBLEM_(d+)_YES_(d+)_$/.test('PROBLEM_5_YES_1_')) {
console.log("Start"); //Start
console.log(RegExp.$2); //
console.log(RegExp.$1); //
console.log("Printed"); //Printed
}





The problem is the regex test is returning true but RegExp.$1 and $2 all are being empty. Values are not being captured.
What am I doing wrong?










share|improve this question

















This question already has an answer here:




  • Alternative to deprecated RegExp.$n object properties

    1 answer




I have the following javascript code running on node.js version 8.9.4 :






if (/^PROBLEM_(d+)_YES_(d+)_$/.test('PROBLEM_5_YES_1_')) {
console.log("Start"); //Start
console.log(RegExp.$2); //
console.log(RegExp.$1); //
console.log("Printed"); //Printed
}





The problem is the regex test is returning true but RegExp.$1 and $2 all are being empty. Values are not being captured.
What am I doing wrong?





This question already has an answer here:




  • Alternative to deprecated RegExp.$n object properties

    1 answer







if (/^PROBLEM_(d+)_YES_(d+)_$/.test('PROBLEM_5_YES_1_')) {
console.log("Start"); //Start
console.log(RegExp.$2); //
console.log(RegExp.$1); //
console.log("Printed"); //Printed
}





if (/^PROBLEM_(d+)_YES_(d+)_$/.test('PROBLEM_5_YES_1_')) {
console.log("Start"); //Start
console.log(RegExp.$2); //
console.log(RegExp.$1); //
console.log("Printed"); //Printed
}






javascript node.js regex






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 6:52









Yosvel Quintero

11.3k42430




11.3k42430










asked Nov 22 '18 at 6:44









aidonsnousaidonsnous

5901825




5901825




marked as duplicate by Wiktor Stribiżew javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 8:51


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Wiktor Stribiżew javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 8:51


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1





    I guess - test doesn't capture group values in js. Try using exec

    – user2932057
    Nov 22 '18 at 6:49














  • 1





    I guess - test doesn't capture group values in js. Try using exec

    – user2932057
    Nov 22 '18 at 6:49








1




1





I guess - test doesn't capture group values in js. Try using exec

– user2932057
Nov 22 '18 at 6:49





I guess - test doesn't capture group values in js. Try using exec

– user2932057
Nov 22 '18 at 6:49












1 Answer
1






active

oldest

votes


















0














The RegExp.$1-$9 properties are non-standard - as MDN says:




Non-standard



This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.




So, it's not surprising that it doesn't work in some implementations.



RegExp refers to the global regular expression object, which is of course not the same thing as the test you just carried out. If you want to use the results of a match, you should use the .match method on the string (or exec on the pattern), and use the indicies of the resulting match object (with bracket notation and numeric indicies):






const input = 'PROBLEM_5_YES_1_';
const match = input.match(/^PROBLEM_(d+)_YES_(d+)_$/);
if (match) {
console.log(match[1]);
console.log(match[2]);
}





In Javascript, dollar signs followed by a number only have a meaning in a replacer function, where $1, for example, will be replaced with the first captured group:






console.log(
'foo bar'.replace(/(foo) (bar)/, '$2 $1')
);








share|improve this answer
































    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    The RegExp.$1-$9 properties are non-standard - as MDN says:




    Non-standard



    This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.




    So, it's not surprising that it doesn't work in some implementations.



    RegExp refers to the global regular expression object, which is of course not the same thing as the test you just carried out. If you want to use the results of a match, you should use the .match method on the string (or exec on the pattern), and use the indicies of the resulting match object (with bracket notation and numeric indicies):






    const input = 'PROBLEM_5_YES_1_';
    const match = input.match(/^PROBLEM_(d+)_YES_(d+)_$/);
    if (match) {
    console.log(match[1]);
    console.log(match[2]);
    }





    In Javascript, dollar signs followed by a number only have a meaning in a replacer function, where $1, for example, will be replaced with the first captured group:






    console.log(
    'foo bar'.replace(/(foo) (bar)/, '$2 $1')
    );








    share|improve this answer






























      0














      The RegExp.$1-$9 properties are non-standard - as MDN says:




      Non-standard



      This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.




      So, it's not surprising that it doesn't work in some implementations.



      RegExp refers to the global regular expression object, which is of course not the same thing as the test you just carried out. If you want to use the results of a match, you should use the .match method on the string (or exec on the pattern), and use the indicies of the resulting match object (with bracket notation and numeric indicies):






      const input = 'PROBLEM_5_YES_1_';
      const match = input.match(/^PROBLEM_(d+)_YES_(d+)_$/);
      if (match) {
      console.log(match[1]);
      console.log(match[2]);
      }





      In Javascript, dollar signs followed by a number only have a meaning in a replacer function, where $1, for example, will be replaced with the first captured group:






      console.log(
      'foo bar'.replace(/(foo) (bar)/, '$2 $1')
      );








      share|improve this answer




























        0












        0








        0







        The RegExp.$1-$9 properties are non-standard - as MDN says:




        Non-standard



        This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.




        So, it's not surprising that it doesn't work in some implementations.



        RegExp refers to the global regular expression object, which is of course not the same thing as the test you just carried out. If you want to use the results of a match, you should use the .match method on the string (or exec on the pattern), and use the indicies of the resulting match object (with bracket notation and numeric indicies):






        const input = 'PROBLEM_5_YES_1_';
        const match = input.match(/^PROBLEM_(d+)_YES_(d+)_$/);
        if (match) {
        console.log(match[1]);
        console.log(match[2]);
        }





        In Javascript, dollar signs followed by a number only have a meaning in a replacer function, where $1, for example, will be replaced with the first captured group:






        console.log(
        'foo bar'.replace(/(foo) (bar)/, '$2 $1')
        );








        share|improve this answer















        The RegExp.$1-$9 properties are non-standard - as MDN says:




        Non-standard



        This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.




        So, it's not surprising that it doesn't work in some implementations.



        RegExp refers to the global regular expression object, which is of course not the same thing as the test you just carried out. If you want to use the results of a match, you should use the .match method on the string (or exec on the pattern), and use the indicies of the resulting match object (with bracket notation and numeric indicies):






        const input = 'PROBLEM_5_YES_1_';
        const match = input.match(/^PROBLEM_(d+)_YES_(d+)_$/);
        if (match) {
        console.log(match[1]);
        console.log(match[2]);
        }





        In Javascript, dollar signs followed by a number only have a meaning in a replacer function, where $1, for example, will be replaced with the first captured group:






        console.log(
        'foo bar'.replace(/(foo) (bar)/, '$2 $1')
        );








        const input = 'PROBLEM_5_YES_1_';
        const match = input.match(/^PROBLEM_(d+)_YES_(d+)_$/);
        if (match) {
        console.log(match[1]);
        console.log(match[2]);
        }





        const input = 'PROBLEM_5_YES_1_';
        const match = input.match(/^PROBLEM_(d+)_YES_(d+)_$/);
        if (match) {
        console.log(match[1]);
        console.log(match[2]);
        }





        console.log(
        'foo bar'.replace(/(foo) (bar)/, '$2 $1')
        );





        console.log(
        'foo bar'.replace(/(foo) (bar)/, '$2 $1')
        );






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 22 '18 at 6:52

























        answered Nov 22 '18 at 6:47









        CertainPerformanceCertainPerformance

        88.9k154977




        88.9k154977

















            Popular posts from this blog

            android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

            SQL update select statement

            'app-layout' is not a known element: how to share Component with different Modules