Objective:
At first I was refusing to learn the regular expression, in the summer vacation to see the class of PHP in the course of a chapter is dedicated to the regular, and then I think for a few seconds, just skipped. The reason is like everybody, fear, listen to a lot of people say it is difficult, and then decisively give up. But yesterday I saw a topic, ask why you do not like the regular, and then see the following many big God said that the regular processing string is very useful, useful, very useful, even some people say that is very interesting! Well, since the useful things, you must learn, must learn, must learn! I thought that when I studied microeconomics this course was rejected, headache, but later in the final exam to find it fun. Therefore, any thing, as long as the intention to invest, will certainly reap the fun.
What is RegExp?
An expression that describes a string rule.
Grammar
- Direct Volume Syntax:/pattern/attrs
- Syntax for creating objects: New RegExp (Pattern,attrs)
Parameter pattern is a string that specifies the pattern of a regular expression or other regular expression.
The parameter attributes is an optional string that contains the property "G" for the global match, and "I" for case-insensitive matching .
RegExp Object Methods
Regexpobj.test (str)
Determine if the regular expression matches the specified string. Returns true if the specified string contains a regular expression, otherwise false.
Regexpobj.exec (str)
More powerful search, more detailed results: Index, status of the process: lastindex
Anchor Point
Used to match a position.
- ^ Indicates the starting position
- $ = End Position
- \b Denotes word boundaries
Character class
Match one of a class of characters
- [ABC] denotes a or B or C
- [0-9] represents any number from 0 to 9
- [^0-9] denotes a character that is not a number, where ^ denotes a non-meaning
- [A-z] denotes any letter from A to Z
- . Represents any one character (except for line breaks)
Metacharacters
Characters that have special meanings
- \d is equivalent to [0-9], which means finding a number
- \d is equivalent to [^\d], which means finding a non-numeric
- \s means finding white space characters
- \s equivalent to [^\s], which means finding non-whitespace characters
- \w is equivalent to [a-za-z0-9], which means finding the word character
- \w equivalent to [^\w], which means finding non-word characters
Quantifiers
Indicates the number of occurrences
- {M,n} represents m to n times
- * indicates {0,} that is 0 to any time
- ? means {0,1} is 0 or 1 times
- + means {1,...} that is 1 to any time
Example:
/^1\d{10}$/to match a string that starts with 1 and has a total of 11 digits
Escape character
The characters that need to be matched are special characters, so you need to escape.
Example:
The/@163\.com$/represents a string ending with @163.com, where. is a special character that needs to be escaped
Multi-Select Branch
or by | means
Example:
/b (a|e) d/===/b[ae]d/means match bad or bed
/\. (png|jpg|gif|jpeg) $/means matching picture format
Character classes are a special branch of multiple selection
Capture
Save the string that matches to it for later reuse.
() means capture
(? :) means not capturing
Methods for String objects that support regular expressions
Str.match (RegExp)
Get a matching string
Str.replace (regexp/substr,replacement)
Replace the substring that matches the regular expression.
BTW: the matching of regular expressions is done only once , so the attribute G is used to represent the global schema.
RegExp Getting Started notes