Introduction to Regular Expressions
What is a regular expression
Regular expressions: Expressions used to match rules. Regular expressions started as an early study of how the human nervous system works and are now widely used in programming languages. Regular tables are often used to retrieve and replace text that conforms to a pattern (rule). A regular expression is a logical formula for manipulating strings. It uses predefined specific characters and their combination to form a rule string. The rule string is used to express the filtering logic for strings.
Functions of regular expressions
- Does the given string conform to the filter logic of the regular expression (match)
- We can use a regular expression to get the specific part of the string that we want (extract)
- Powerful string substitution (substitution)
Features of regular expressions
- Very flexible, logical and functional
- Complex control of strings can be achieved quickly and in a very simple manner
- For new people, it is obscure and difficult to understand
Composition of a regular expression
- Normal character
- Special characters (metacharacters) : Characters that have special meanings in a regular expression
metacharacters
Common meta-string
metacharacters | instructions |
---|---|
\d | Match the Numbers |
\D | Matches any non-numeric character |
\w | Matches letters or numbers or underscores |
\W | Matches any other letter, number, or underscore |
\s | Matches any whitespace |
\S | Matches any character that is not whitespace |
. | Matches any single character other than a newline character |
^ | Represents the text at the beginning of the line to match (who starts with) |
$ | Represents the text at the end of the line that matches (who ends with) |
qualifiers
qualifiers | instructions |
---|---|
* | Repeat zero or more times |
+ | Repeat one or more times |
? | Repeat zero or one time |
{n} | Repeated n times |
{n,} | Repeat n or more times |
{n,m} | Repeat n to m times |
other
Enclosed in brackets [] string, means to match any of these characters, the meaning of is equal to or [^] match in addition to the content within the brackets \ escape characters | or, choose one of the two. Note | left and right sides can be divided into two parts, and no matter how long the left and right sides more random () choose one of the amount directly from two, grouping eg: wh matching whay and whey (a) | e y \ u4e00 - \ u9fa5 matching Chinese charactersCopy the code
demo
Verify phone number:
^\d{11} $Copy the code
Verify zip code:
^\d{6} $Copy the code
Verification email: [email protected]
^\w+@\w+\.\w+$
Copy the code
Regular expressions are used in JavaScript
Creating regular objects
Method 1:
var reg = new Regex('\\d'.'i');
var reg = new Regex('\\d'.'gi');
Copy the code
Method 2:
var reg = /\d/i;
var reg = /\d/gi;
Copy the code
parameter
mark | instructions |
---|---|
i | Ignore case |
g | The global matching |
gi | Global match + ignore case |
Regular match
// Match the date
var dateStr = '2019-04-10';
var reg = / ^ \ d {4} \ d {1, 2} - \ d {1, 2} $/
console.log(reg.test(dateStr));
Copy the code
Regular extraction
// 1
var str = "Zhang SAN: 1000, Li Si: 5000, Wang Wu: 8000.";
var array = str.match(/\d+/g);
console.log(array);
// 2. Extract the email address
var str = "[email protected],[email protected] [email protected] 2、[email protected] [email protected]...";
var array = str.match(/\w+@\w+\.\w+(\.\w+)? /g);
console.log(array);
// 3. Group extraction
// 3. The year part of the date is 2015-5-10
var dateStr = '2016-1-5';
Regex.$1 $2 $3.... In order to get
var reg = / (\ d {4}) - \ d {1, 2} - \ d {1, 2} /;
if (reg.test(dateStr)) {
console.log(RegExp. $1);
}
Copy the code
Regular replacement
// 1. Replace all whitespace
var str = " 123AD asadf asadfasf adf ";
str = str.replace(/\s/g."xx");
console.log(str);
/ / 2. Replace all, |.
var str = "ABC, EFG,123, ABC,123, a";
str = str.replace(/ g /, |.".");
console.log(str);
Copy the code
Tidy up the small demo, you can expand
var str = 'b';
// var reg = /\d/; // As long as there is a number in the string, it meets the requirement
// var reg = /\D/; // As long as there are non-numbers in the string, it meets the requirement
// var reg = /\w/; // As long as there are digits or letters in the string, or underscores, it meets the requirement
// var reg = /\W/; // As long as there are digits or letters in the string, or underscores, it meets the requirement
// var reg = /\s/; // As long as there are whitespace characters in the string
// var reg = /\S/; // As long as there are non-whitespace characters in the string
// var reg = /./; // If there is a non-newline character, the condition is met
// var reg = /^a/; // This is true as long as the string begins with a
// var reg = /a$/; // This is true as long as the string ends in a
// var reg = /^abc$/; // Requires only ABC in the string
// var reg = /^\d{11}$/; // Requires that only 11 numbers appear in the string
// var reg = /^\d{3,}$/; // Requires the number to appear more than three times in the string
// var reg = /\d{3,}/; // If the string contains three or more digits, the condition is met
Var reg = /^\d{3,5}$/; // The number in the string can occur only 3 to 5 times
// var reg = /^\d*$/; // The string is either nothing or only numbers
// var reg = /^\d+$/; // At least one number in the string
// var reg = /^\d? $/; // There is either nothing or only one number in the string
// var reg = /^[abcd]$/; // There can be only one character, which can be either of the brackets
// var reg = /^[^abcd]$/; // There can be only one character, and this character can have either bracket or not
// var reg = /^\.$/; // There can be only one dot in the string
// var reg = /^a|c$/; // Start with a and end with c
/ / var/reg = ^ wh (a) | g y $/ / / or whay whgy
// var reg = /^[a-Z]$/; / / error
// var reg = /^[a-zA-Z]$/;
// var reg = /^[A-z]$/; // Uppercase letters are sorted earlier in the character set
console.log(reg.test(str));
Copy the code
conclusion
- Simple regular expressions, this thing, it is very simple, complex complex, in the final analysis, or not familiar with, or forget, when I find, but over a period of time many would forget, because most of the cases are Google, so if you want to, that can only be practiced more, more, and then forget, It’s easy to pick up an impression.
- Go too far, look back will find a lot of harvest