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;
}
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
add a comment |
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
add a comment |
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
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
regex
edited Jan 3 at 9:28
Josh Habdas
3,1492634
3,1492634
asked Aug 12 '11 at 14:09


gb2dgb2d
3,41264183
3,41264183
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
To match multiple times use to need use the global option
str.match(/your_expression_here/g)
^
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
add a comment |
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.
add a comment |
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.
While you're at it, change eachd*
tod+
; 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
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%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
To match multiple times use to need use the global option
str.match(/your_expression_here/g)
^
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
add a comment |
To match multiple times use to need use the global option
str.match(/your_expression_here/g)
^
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
add a comment |
To match multiple times use to need use the global option
str.match(/your_expression_here/g)
^
To match multiple times use to need use the global option
str.match(/your_expression_here/g)
^
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
add a comment |
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Jan 3 at 9:21
Josh HabdasJosh Habdas
3,1492634
3,1492634
add a comment |
add a comment |
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.
While you're at it, change eachd*
tod+
; 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
add a comment |
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.
While you're at it, change eachd*
tod+
; 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
add a comment |
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.
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.
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 eachd*
tod+
; 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
add a comment |
While you're at it, change eachd*
tod+
; 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
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.
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%2f7041308%2fhow-to-match-multiple-occurrences-of-a-substring%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