js regular expression learning
1. What is a regular expression?
-
Regular expressions describe the composition pattern of strings and are often used to check whether strings meet predetermined format requirements
-
**Regular expressions describe rules bit by bit: * * it describes the form of strings bit by bit
<script> // Create regular expression rule: indicates whether the detection data is a string starting with m and ending with n, with 6 numbers in the middle var regexp = /^m\d{6}n$/; var a="m123456n"; // test method using regular expressions ceshi console.log(regexp.test(a));//The result is true, which complies with the rules </script>
2. Creation of regular expressions
- Method 1: create a regular expression in the form of / content /
- Method 2: create a regular expression in the form of new RegExp('content '), but pay attention to escape characters\
- js has a built-in RegExp() constructor
- Use the typeof operator to detect the type of regular expression, and the result is object
3. Metacharacter
-
A metacharacter is a character of a specified type
- \d: Match one digit
- \D: Match a non digit
- \w: Match one single word character (letter, number, or underscore)
- \W: Match a non single word character
- \s: Matches a blank character (space, tab, or newline)
- .: any character
- ^: indicates the beginning of the match
- $: indicates the end of the match
- [abc]: create a character set that can be matched
- {5} : quantifier, indicating the preceding characters * 5
-
When we use new RegExp() to create regular expressions, we should pay attention to adding a backslash to escape where \ is used
-
Adding a \ before a special character means that the next character is not a special character and should be understood literally
-
var regExp=/^.$/;//. represents any character var regExp=/^\.$/;//. represents. Instead of any character
-
Whether a symbol is a special character or not, you can add a \ before the symbol to ensure that it represents the symbol itself
4. Square bracket notation
- Using the square bracket notation, you can create a character set to match any of the characters, such as: [abc], which means that as long as it is a or b or c, it will pass
More advanced use of square brackets
- You can use - in square brackets to represent a range, and ^ in square brackets means negative, which means non
- The ranges in square brackets are or operations
- \d is equivalent to [0-9]
- \D is equivalent to [^ 0-9]
- \w is equivalent to [A-Za-z0-9_]
- \W is equivalent to [^ A-Za-z0-9]
<script> // Title 1: a five digit string, which is composed of upper and lower case letters var regexp=/^[A-Za-z]{5}$/; // Title 2: the string is five digits and consists only of lowercase letters and dots var regexp2=/^[a-z\.]{5}$/; // Title 3: four lowercase letters, and the last digit cannot be m var regexp3=/^[a-z]{3}[a-ln-z]$/ // var regexp3=/^[a-z]{3}[a-z^m] $/ is incorrect. Because a-z already contains M, it is considered true as long as one of them meets the specification </script>
5. Quantifiers
Quantifier: a word indicating the number of occurrences of the previous expression
- *: match the previous expression 0 or more times, equivalent to {0,}
- +: match the previous expression one or more times, equivalent to {1,}
- ?: match the previous expression 0 or 1 times, equivalent to {0,1}
- {n} : matches the previous expression exactly n times
- {n,}: matches the previous expression at least N times
- {n,m}: match the previous expression at least N times and at most m times
<script> // Verify the eleven digit mobile phone number. The first character must be 1 and the last ten digits must be numbers var regexp=/^1\d{10}$/; var a=54571927712;//false var b=15457192771;//true console.log(regexp.test(a));//false console.log(regexp.test(b));//true // Validation string: starts with a letter, with any number in the middle (at least 1 bit) and ends with a letter var regexp1=/^[A-Za-z]\d+[A-Za-z]$/; var c="a1515b";//true var d="122121b";//false console.log(regexp1.test(c));//true console.log(regexp1.test(d));//false // Verification website address: start with www., with any number of characters in the middle, and end with. com or. com.cn var regexp2=/^www\.\w{1,}\.com(.cn)?$/;//? indicates that the preceding character can be left blank, so enclose. cn to indicate that. cn can be left blank </script>
6. Modifier
-
Modifiers, also known as flags, are used for advanced search using regular expressions
- i: Case insensitive search
- g: Global search
-
How to use modifiers:
-
// Add modifiers to regular expressions var regexp=/m/gi; // Separate new expressions with commas var regexp1=new RegExp("m","gi");
-