Although "?" The appearance of the three metacharacters "+" and "*" solves many problems. However, they are not perfect: 1. there is no way to specify the maximum number of matching characters. For example, if we want to match a mobile phone number, it should be 11 digits, while "+" and "*" will match as many numbers as possible, 17 or 18 are correct. 2. There is no way to specify the minimum number of matching characters, such as "+", "*", and "?". The minimum number of characters that can be provided is either zero or one.
In a regular expression, it can be in a single character (such as "j"), a character group (such as "[abcde]"), or a specific character type (such as "\ d "), any character (that is, ". ") followed by" {number} ", to match a string consisting of zero or multiple characters.
For example, "\ d {3}" can be used to match the number from 999 to 1000. When "a {6}" is used, it can match "aaaaaa" (or it can only match, because "a" is a fixed character ).
Now let's consider a more complex example. If we want to match the mobile phone number, the rule is: the first is "1", and the second is "3 or 5 ", any number with nine digits. Then its matching should be like this:
1[35][0-9]{9}
Effect demonstration
13536781234
18677530289
15345231123
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>");}
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