首先,什么事正则表达式呢,其实引入概念很多时候并不能帮我们明白它到底是什么,所以我先简单描述下,正则表达式,其实就是一个记录字符串规则则的字符串,等我们看完这一部分,也就能明白它到底是什么了。
基本语法:正则表达式就是“/expression/”+表示搜索范围的符号。例如我们要找function这个关键词,就是/function/gi,其中g表示global,就是全局搜索,i表示ignor,就是忽略大小写。
在js中,我们通过RegExp类来实现。
这个类里面有很多很多的符号用来表示不同的索引方法,我先把这个表列一列:
元字符 |
说明 |
量词 |
说明 |
反义字符 |
说明 |
. |
匹配除了换行符号(\n)以外的任意字符 |
* |
出现次数:[0,+∞) |
\W |
字母,数字,下划线,汉字以外的字符 |
^ |
匹配字符串的开始 |
+ |
出现次数:[1,+∞)
|
\S |
空白字符以外的字符 |
$ |
匹配字符串的结束 |
? |
出现次数:[0,1] |
\D |
数字以外的字符 |
\b |
匹配单词边界 |
{n} |
出现次数:n |
\B |
匹配非单词的边界 |
\d |
匹配数字 |
{n,} |
出现次数:[n,+∞) |
[^] |
在字符类中,^号后面的字符串以外的任意字符 |
\s |
匹配任意的空白符 |
{n,m} |
出现次数:[n,m] |
[-] |
匹配从-前字符到-后字符的字符串中的字符/数字 |
\w |
匹配字母,数字,下划线或汉字 |
|
|
|
|
In addition to the above symbols, there are three concepts: one is grouping, one is backreference, and the last is candidate.
Grouping: refers to enclosing strings with (), so that strings can be combined according to certain rules. At the same time, brackets can also be nested, such as using regular expressions to express the date format: var dateReg=/^(d{1,4})(-)(d{1,2}(-)(d{1 ,2})$), Of course, this method also has certain loopholes. Here we just express the format problem.
In addition to these, there are square brackets. For example, if you only want to match one of the letters x y z d w, then write [xyzdw]. If the match is continuous, such as numbers 0-4, then [0 -4], but this only represents one character.
If you want to write more than one, such as matching ac or bd, then use the "|" symbol and write (ac|bd).
For example, if we want to match a string containing only abc, we can write: abdReg=/^[abc] $/; The following is a complete example.
regular express
Reverse Directed reference: It is an application of regular expressions based on grouping. First of all, you need to know the group number: follow the order of the groups from left to right, marked by the left bracket, and start accumulating from 1. There are two ways to use it: $group number, or group number.
The second one is suitable for referencing groups in expressions. "" is an escape symbol, which has the same meaning as usual. It is used when matching reserved characters.
For example, we want to match a string that starts with abc, ends with abc, and has no limit in the middle: Reg=/^(abc)[a-z]*1$/; You can try this sentence in the example just now, I Tested and there were no errors.
Several commonly used matching regular expressions:
1. Matching date: reg=/^d{4}-(((0[13578]|(10|12))-(0[1-9] |[1-2]d|3[01]))|(02-(0[1-9])|[1-2]d)|((0[469]|11)-(0[1- 9]|[1-2]d|30)))$/g;
2. Matching time: reg=/^([0-1]d|[2][0-3](:([ 0-5]d)){2}$/g;
3. Matching email address: reg=/^([a-zA-Z]([0-9a-zA-Z]|(_))* @(([0-9a-zA-Z]|(_)) .) [a-zA-Z]{2,9})$/g;
4. Match Chinese characters: reg=/^[ u4e00-u9fa5] $/g;
Javascript operates cookies
I believe everyone knows what cookies are, so I just copied the definition without mercy.
The statements for operating cookies with js are as follows: document.cookie=name "="value ";expires=" date.toGMTString();
Next we will use cookies to record the user name and password for login