Now let's review the above greedy and inert matching syntax tables. Two matching modes are interesting:
One is "{n}". For this form of matching, it does not matter whether it is greedy or inert because it precisely requires matching n characters, even though "{n }?" It is also the correct matching method, but its matching result is always the same as "{n.
Another one is "?", It looks odd and hard to understand, because we usually use greedy matching to match multiple matches, such as "*" or "+, here is 0 or 1. What is its greedy and inert match? Let's take a look at the example to illustrate:
The regular expression is as follows:
bkjia?
Effect demonstration welcome to bkjia
This example code
function reg_replace(){var test = document.getElementById("test");aim = "bkjia?";var regex = new RegExp("("+aim+")","g");test.innerHTML = test.innerHTML.replace(regex,"<span style='background-color:orange'>$1</span>");}
In this match, the match is as follows: first, we need to match the "bkjia" string, And Then There can be 0 or 1 "s ", as described above, as many greedy matches as possible, bkjia is matched.
Let's take a look at "?". This match.
The regular expression is as follows:
bkjia??
Effect demonstration welcome to bkjia
This time only matches "nowamagi". I don't think you need to explain it too much. You have understood this weird matching syntax. The "nowamagi" found that the "nowamagi" matches 0 conditions. Therefore, it is saved to the matching result set and matched again until the string ends.
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