We will consider the matching of 000-999 again: "\ d {3}". Although it is correct, it can only match exactly 3 bits: 000, 001, 002... 100, 101... 999. Generally, we need to match numbers such as 0, 10, and 99. In this case, we need to specify numbers that can match one to three digits.
In a regular expression, the "{minimum, maximum}" syntax is used. It is used in the same way as the syntax described in the previous section that matches a fixed number of characters.
[0-9]{1,3}
Effect demonstration
100
001
1xxx
1001
1c
This example code
function reg_replace(){var test = document.getElementById("test");aim = "1[35][0-9]{9}";var regex = new RegExp("("+aim+")","g");test.innerHTML = test.innerHTML.replace(regex,"<span style='background-color:orange'>$1</span>");}
We found two problems: 1. We do not want "1234" to be matched. However, it is divided into two parts: "123" and "4" for matching, how can we prevent it from being matched? 2. Why is it divided into 123 and 4 instead of 1 and 234? We will leave it for further discussion.
Note two special cases:
- The minimum number can be 0, so "{0, 1}" is equivalent to "?".
- If you do not limit the maximum number, you can set it to null. Therefore, "\ d {1,}" is equivalent to "+", and "{0,}" is equivalent to "*".
"{" And "}" are also metacharacters. When we need to match them, use "\" for escape: "\ {" 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