Today we share some knowledge of regular expressions and their application in JavaScript. The regular expression is simple but not simple, for example, my teacher told us about this thing in the beginning of the word twenty or thirty minutes to master, once there is no entry that can be a few days to fill back. So it was very serious study and study it. Did not expect the regular expression not only the code is concise, but also in the actual operation for the front-end engineers to save a lot. It is well known that when users browse the page, the only way to deal with the data is the form, about the validation of the form, there are many methods, and then, I will give you two, one is the common cumbersome method, one is regular expression, see what it can bring to the form of the effect of it.
First look at the normal version:
The personal feeling method is too earthy, this is the simple form effect that is just started to study, does not add the CSS style sheet.
Take a look at the regular expressions for the upgraded version:
How about a lot of brevity?
let's look at the regular expressions together.
A regular expression is an object that describes a character pattern, also known as a formal representation, a regular representation
A regular expression uses a single string to describe and match a series of strings that conform to a certain syntactic rule. In many text editors, regular expressions are often used to retrieve and replace text that conforms to a pattern.
Regular expressions are characterized by:
1. Flexibility, logic and functionality are very strong;
2. Complex control of strings can be achieved quickly and in a very simple way.
3. For people who have just come into contact, it is more obscure and difficult to understand.
Defining Regular Expressions
1: Normal Way
var reg=/expression/Additional parameters
Reg.test (V.value)
2: Constructor mode
var reg=new RegExp ("expression", "Additional parameters")
var reg=new RegExp ("China");
The pattern of an expression
1: Simple Mode
var reg=/china/;
2: Compliant mode
var reg=/^\w+$/;
var reg=/^\[email protected]\w+. [A-za-z] {2,3} (. [A-za-z] {2,3})? $/;
Methods for RegExp objects
The Exec search character is a wildcard of regular expressions, returns the found value, and determines its location
Test retrieves the value specified in the string, returning True or false
Exec method:
If no match is found, the return value is null, and if a match is found, a result array is returned.
/.../represents the beginning and end of a pattern
^ Start of matching string
$ match End of string
\s any whitespace characters
\s any non-whitespace characters
\d matches a numeric character, equivalent to [0-9]
\d any character other than a number, equivalent to [^0-9]
\w matches a number, underscore, or alphabetic character, equivalent to [a-za-z0-9_]
\w any non-single character, equivalent to [^a-za-z0-9_]
. Any character other than a line break
{n} matches the previous item n times
{N,} matches the previous item n times, or multiple times
{N,m} matches the previous item at least n times, but cannot exceed m times
* matches the previous item 0 or more times, equivalent to {0,}
+ matches the previous item 1 or more times, equivalent to {1,}
? Matches the previous item 0 or 1 times, which means the previous item is optional, equivalent to {0,1}
X|y
Match x or Y. For example, "Z|food" can match "Z" or "food". "(z|f) Ood" matches "Zood" or "food".
[XYZ]
The character set is combined. Matches any one of the characters contained. For example, "[ABC]" can Match "a" in "plain".
[A-z]
The character range. Matches any character within the specified range. For example, "[A-z]" can match any lowercase alphabetic character in the range "a" to "Z".
Note: The range of characters can be represented only if the hyphen is inside a character group and appears between two characters; If the beginning of the character group is out, only the hyphen itself can be represented.
(pattern)
Match pattern and get this match. The obtained matches can be obtained from the resulting matches collection, the Submatches collection is used in VBScript, and the $0...$9 property is used in JScript. To match the parentheses character, use "\ (" or "\").
There are a lot of rules about regular expressions, just a few of them, and in the days ahead, let's explore.
The concept and application of the expression in JavaScript