A regular expression is a logical formula for string manipulation, a "rule string" with some predetermined specific characters, and the string is filtered with a "rule string".
ECMAScript support for regular expressions by RegExp type
Purpose: Whether the given string conforms to the filtering logic of the regular expression, called: match;
you can get the specific part we want from the string through regular expressions, called: find;
three functions of regular expressions:
1, fast matching to make a string;
2. Replace strings that follow regular expression rules;
3. Filter the specified string in the target string
syntax for regular expressions:
A regular expression is a text pattern consisting of ordinary characters (for example: A~z) and metacharacters (special characters) that describe one or more strings to match when looking for a text body, and a regular expression as a template that matches a string pattern to the searched string.
Create a regular expression object (regular expression: regular expression)
var regExp =
New RegExp (pattern, attributes);
the constructor used when constructing the regular expression object has two parameters
1. The parameter pattern is a string that specifies the pattern of the regular expression or other regular expression;
2. Parameter attributes is an optional string
: A total of three modes, these three modes can be combined with
I: case insensitive;
g: Global lookup, within the entire target string range;
m: Multi-line search;
shorthand form for regular expressions: literal form
The double slash includes the text to be matched.
a double slash followed by a pattern combination
var regExp =/a/gi;
the test method provided by the regular expression object is used to detect if the argument string has text that conforms to the expression template, and returns True if any, and false if none;
For example:
var str = "Love Me Love My Dog";
reg = new RegExp ("Love", "GI");
regExp =/love/gi;
1. If you want to match whether the target string starts with love,
It needs to be matched by a meta-character ^ (caret) to match whether the beginning contains a regular expression template
regExp =/^love/gi;
2, to match the end by the meta-character $ to match
regExp =/^love$/gi;
3, \s is a blank character, (S:spance) is an escape, similar to \ n (newline character);
regExp =/love\s/gi;
4, match the target to be able to be a word, can use \w (W to capitalize);
regExp =/love\w/gi;
Console.log (Reg.test (str));
5, if you want to match the number, you need to use the escape character \d, d:digital that is the meaning of the number
regExp =/love\d/gi;
regExp =/[a-z].\d+\w/gi;
6. Vertical Bars in Elements |
str = "22334455";
regExp =/22|55/g;
Console.log ("| meta-character:" + regexp.test (str));
7. Repeating class setting consecutive repetitions {}
str= ' 211111161 ';
number of consecutive repetitions {}
REGEXP =/1{7}/g;//set to repeat 7 times consecutively
minimum number of consecutive repetitions {2}
REGEXP =/1{2,}/g;//set minimum continuous repetition 2 times
repeat at least 2 times, up to 4 times {2,4}
REGEXP =/1{2,4}/g;//set at least 2 consecutive repetitions up to 4 times
8, the meaning of the meta-character + for the matching target at least consecutive occurrences, equivalent to {1,};
regExp =/1+/g;
9, meta-characters? , up to one time, equivalent to {1,0}
regExp =/1?/g;
10, the character class [];
regExp =/[1]/g; Match any one of the square brackets
11. Hyphen-
matches any number in the range 0 to 4
regExp =/[0-4]/g;
12, ^ and [] with the exception of the meaning
str = ' aaa123bb123add123 ';
regExp =/[^123]/g; Items other than 123 are matched
str = "Abcfhglz";
regExp =/[a-z]/gi;
regExp =/[a-za-z]/g; Match letters include case
13. Match Chinese characters in string
str = ' today is Tuesday ';
regExp =/[\u4e00-\u9fa5]+/g;
Console.log (Str.match (REGEXP));
Example: replacing the short term in 2016-08-16 with a point
Html,javascript, Regular expression