Greedy)
It will match as many characters as possible. It first looks at the entire string. If it does not match, the string is reduced. If it encounters a possible match, it stops shrinking and expands the text. When a matching text is found, it does not rush to save the match to the matching set, but expands the text until it cannot continue matching or expand the complete string, save the last matched text (also the longest) to the matching set. So it is greedy.
Looking at the above definition, it is difficult for us to have a vivid understanding. Now we suppose we want to match the text between <B> and </B> below. For demonstration, although it does not conform to the HTML definition, we add a text between <B> and </c>:
Bkjia.com is a <B> good </B> website to <B> learn </B> IT <B> skills </c>.
<b>.*</b>
Effect demonstration bkjia.com is a good website to learn IT skills.
This example code
function reg_replace(){var test = document.getElementById("test");aim = "<b>.*</b> ";var regex = new RegExp("("+aim+")","g");test.innerHTML = test.innerHTML.replace(regex,"<span style='background-color:orange'>$1</span>");}
This matching is obviously not our original intention. It only finds one matching, and usually, what we want to get is a match between <B> junior </B> and <B> living </B>.
Greedy matching process
// Mismatch. Shrink bkjia.com is a <B> good </B> website to <B> learn </B> IT <B> skills </c>. // mismatch. Contract owamagic.net is a <B> good </B> website to <B> learn </B> IT <B> skills </c>. // mismatch. Shrink wamagic.net is a <B> good </B> website to <B> learn </B> IT <B> skills </c> .... // omitted in the middle // locate possible matches and extend <B> good </B> website to <B> learn </B> IT <B> skills </c>. // find the possible match and extend <B> good </B> website to <B> learn </B> IT <B> skills </c>. // find the possible match and extend <B> good </B> website to <B> learn </B> IT <B> skills </c> .... // omitted in the middle // find a match, but it is not saved to the result set, instead, expand <B> good </B> website to <B> learn </B> IT <B> skills </c>. <B> good </B> website to <B> learn </B> IT <B> skills </c>. // find the possible match and extend <B> good </B> website to <B> learn </B> IT <B> skills </c>. // string ends, save the last matched <B> good </B> website to <B> learn </B> to the matching result set <B> good </B> website <B> learn </B> IT <B> skills </c>.
Additional reading
The topic list of this article is as follows:
- What is a regular expression?
- Getting started with regular expressions: match a Fixed Single Character
- Getting started with regular expressions: matching any single character
- Getting started with regular expressions: Use character groups
- Getting started with regular expressions: Use character ranges in character groups
- Getting started with regular expressions: Use of assense character groups
- Getting started with regular expressions: matching null characters
- Getting started with regular expressions: Match one or more characters
- Regular Expression: matches zero or multiple characters.
- Regular Expression entry: matches zero or one string.
- Getting started with regular expressions: Match fixed numbers of Characters
- Getting started with regular expressions: match the number of characters in a range
- Getting started with regular expressions: greedy matching
- Getting started with regular expressions: inert matching
- Entry to Regular Expressions: two matching Modes
- Getting started with regular expressions: match word boundaries
- Getting started with regular expressions: boundary definition and relativity
- Getting started with regular expressions: Match non-word boundaries
- Getting started with regular expressions: match the beginning and end of a text
- Entry to regular expression: submode
- Regular Expression entry: "or" Match
- Getting started with regular expressions: replacing with referenced text
- Getting started with regular expressions: unmatched
- Regular Expression Summary: Regular Expressions in JavaScript
- Regular Expression Summary: advanced application of regular expressions in js