Now we need to match the correctness of words. For the word "flower", whether it is the singular "flower" or the plural "flowers", it is the correct method, flowersss is incorrect.
Using the previous knowledge, the matching cannot be completed:
For "flowers +", it cannot match "flower" because it can only match one or more "s" words after "flower". Therefore, both "flowers" and "flowersss" can be matched.
For "flowers *", although it can match "flower" and "flowers", it can also match "flowersss ".
In a regular expression, use "?" To match zero or one character. It is used in the same way as "+" and.
In this example, the usage is as follows:
flowers?
Effect demonstration
Flower
Flowers
Flowersss
This example code
function reg_replace(){var test = document.getElementById("test");aim = "flowers?";var regex = new RegExp("("+aim+")","g");test.innerHTML = test.innerHTML.replace(regex,"<span style='background-color:orange'>$1</span>");}
Obviously, in the multi-character matching described in this chapter, "+", "*", and "?" They are all metacharacters. to match them, use "\" to escape them: "\ +", "\ *", and "\?".
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