Is it possible in javascript regex, to match string inside the string matched by my capture group?
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
|
show 4 more comments
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
3
Enclose the firstd+
in another group?(d+)
?
– CertainPerformance
Nov 20 at 8:13
If you uses.match(/.../g)
, see How do you access the matched groups in a JavaScript regular expression?
– Wiktor Stribiżew
Nov 20 at 8:18
@CertainPerformance could you provide demo of what you mean? Thank you
– Tornike Shavishvili
Nov 20 at 8:30
@PoulBak If OP usesmatch
withg
, 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 at 8:31
1
@TornikeShavishvili See stackoverflow.com/questions/432493/…
– Ivar
Nov 20 at 9:03
|
show 4 more comments
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
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
javascript regex
edited Nov 20 at 9:16
Poul Bak
5,43331132
5,43331132
asked Nov 20 at 8:12
Tornike Shavishvili
61021022
61021022
3
Enclose the firstd+
in another group?(d+)
?
– CertainPerformance
Nov 20 at 8:13
If you uses.match(/.../g)
, see How do you access the matched groups in a JavaScript regular expression?
– Wiktor Stribiżew
Nov 20 at 8:18
@CertainPerformance could you provide demo of what you mean? Thank you
– Tornike Shavishvili
Nov 20 at 8:30
@PoulBak If OP usesmatch
withg
, 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 at 8:31
1
@TornikeShavishvili See stackoverflow.com/questions/432493/…
– Ivar
Nov 20 at 9:03
|
show 4 more comments
3
Enclose the firstd+
in another group?(d+)
?
– CertainPerformance
Nov 20 at 8:13
If you uses.match(/.../g)
, see How do you access the matched groups in a JavaScript regular expression?
– Wiktor Stribiżew
Nov 20 at 8:18
@CertainPerformance could you provide demo of what you mean? Thank you
– Tornike Shavishvili
Nov 20 at 8:30
@PoulBak If OP usesmatch
withg
, 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 at 8:31
1
@TornikeShavishvili See stackoverflow.com/questions/432493/…
– Ivar
Nov 20 at 9:03
3
3
Enclose the first
d+
in another group? (d+)
?– CertainPerformance
Nov 20 at 8:13
Enclose the first
d+
in another group? (d+)
?– CertainPerformance
Nov 20 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 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 at 8:18
@CertainPerformance could you provide demo of what you mean? Thank you
– Tornike Shavishvili
Nov 20 at 8:30
@CertainPerformance could you provide demo of what you mean? Thank you
– Tornike Shavishvili
Nov 20 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 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 at 8:31
1
1
@TornikeShavishvili See stackoverflow.com/questions/432493/…
– Ivar
Nov 20 at 9:03
@TornikeShavishvili See stackoverflow.com/questions/432493/…
– Ivar
Nov 20 at 9:03
|
show 4 more comments
2 Answers
2
active
oldest
votes
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
2
Thank you. Is there any way to accomplish this with regex? not to use mach twice?
– Tornike Shavishvili
Nov 20 at 8:21
Thematch
twice is just for the code example
– Yosvel Quintero
Nov 20 at 8:22
How would you get the same result with one call ofmatch
?
– Tornike Shavishvili
Nov 20 at 8:26
Check the updated answer.. And just for you to know i have not downvoted your question
– Yosvel Quintero
Nov 20 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 at 9:12
|
show 2 more comments
You could use (mind the case insensitive flag in the demo):
operatorD+(d+)
See a demo on regex101.com.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
2
Thank you. Is there any way to accomplish this with regex? not to use mach twice?
– Tornike Shavishvili
Nov 20 at 8:21
Thematch
twice is just for the code example
– Yosvel Quintero
Nov 20 at 8:22
How would you get the same result with one call ofmatch
?
– Tornike Shavishvili
Nov 20 at 8:26
Check the updated answer.. And just for you to know i have not downvoted your question
– Yosvel Quintero
Nov 20 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 at 9:12
|
show 2 more comments
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
2
Thank you. Is there any way to accomplish this with regex? not to use mach twice?
– Tornike Shavishvili
Nov 20 at 8:21
Thematch
twice is just for the code example
– Yosvel Quintero
Nov 20 at 8:22
How would you get the same result with one call ofmatch
?
– Tornike Shavishvili
Nov 20 at 8:26
Check the updated answer.. And just for you to know i have not downvoted your question
– Yosvel Quintero
Nov 20 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 at 9:12
|
show 2 more comments
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
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
edited Nov 20 at 9:21
answered Nov 20 at 8:19
Yosvel Quintero
11k42330
11k42330
2
Thank you. Is there any way to accomplish this with regex? not to use mach twice?
– Tornike Shavishvili
Nov 20 at 8:21
Thematch
twice is just for the code example
– Yosvel Quintero
Nov 20 at 8:22
How would you get the same result with one call ofmatch
?
– Tornike Shavishvili
Nov 20 at 8:26
Check the updated answer.. And just for you to know i have not downvoted your question
– Yosvel Quintero
Nov 20 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 at 9:12
|
show 2 more comments
2
Thank you. Is there any way to accomplish this with regex? not to use mach twice?
– Tornike Shavishvili
Nov 20 at 8:21
Thematch
twice is just for the code example
– Yosvel Quintero
Nov 20 at 8:22
How would you get the same result with one call ofmatch
?
– Tornike Shavishvili
Nov 20 at 8:26
Check the updated answer.. And just for you to know i have not downvoted your question
– Yosvel Quintero
Nov 20 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 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 at 8:21
Thank you. Is there any way to accomplish this with regex? not to use mach twice?
– Tornike Shavishvili
Nov 20 at 8:21
The
match
twice is just for the code example– Yosvel Quintero
Nov 20 at 8:22
The
match
twice is just for the code example– Yosvel Quintero
Nov 20 at 8:22
How would you get the same result with one call of
match
?– Tornike Shavishvili
Nov 20 at 8:26
How would you get the same result with one call of
match
?– Tornike Shavishvili
Nov 20 at 8:26
Check the updated answer.. And just for you to know i have not downvoted your question
– Yosvel Quintero
Nov 20 at 8:35
Check the updated answer.. And just for you to know i have not downvoted your question
– Yosvel Quintero
Nov 20 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 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 at 9:12
|
show 2 more comments
You could use (mind the case insensitive flag in the demo):
operatorD+(d+)
See a demo on regex101.com.
add a comment |
You could use (mind the case insensitive flag in the demo):
operatorD+(d+)
See a demo on regex101.com.
add a comment |
You could use (mind the case insensitive flag in the demo):
operatorD+(d+)
See a demo on regex101.com.
You could use (mind the case insensitive flag in the demo):
operatorD+(d+)
See a demo on regex101.com.
answered Nov 20 at 8:14
Jan
24.2k52348
24.2k52348
add a comment |
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
3
Enclose the first
d+
in another group?(d+)
?– CertainPerformance
Nov 20 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 at 8:18
@CertainPerformance could you provide demo of what you mean? Thank you
– Tornike Shavishvili
Nov 20 at 8:30
@PoulBak If OP uses
match
withg
, 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 at 8:31
1
@TornikeShavishvili See stackoverflow.com/questions/432493/…
– Ivar
Nov 20 at 9:03