Regex is not capturing value [duplicate]
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?
javascript node.js regex
marked as duplicate by Wiktor Stribiżew
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.
add a comment |
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?
javascript node.js regex
marked as duplicate by Wiktor Stribiżew
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 usingexec
– user2932057
Nov 22 '18 at 6:49
add a comment |
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?
javascript node.js regex
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
javascript node.js regex
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
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
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 usingexec
– user2932057
Nov 22 '18 at 6:49
add a comment |
1
I guess -test
doesn't capture group values in js. Try usingexec
– 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
add a comment |
1 Answer
1
active
oldest
votes
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')
);
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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')
);
add a comment |
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')
);
add a comment |
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')
);
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')
);
edited Nov 22 '18 at 6:52
answered Nov 22 '18 at 6:47
CertainPerformanceCertainPerformance
88.9k154977
88.9k154977
add a comment |
add a comment |
1
I guess -
test
doesn't capture group values in js. Try usingexec
– user2932057
Nov 22 '18 at 6:49