Metacharacter Symbols ^.*?\
(1) ^h表示必须由h开头;
(2)d$表示必须由d结尾;
(3).表示任意一个字符
(4)*表示任意0个或多个字符;
(5)?a?e表示可以匹配a也可以匹配b;
(6)\?表示匹配?;
re = /^h/i; // str Must start with 'h'
re = / world$/i; //Must ends with 'd'
re = /^hello$/i; //Must start with 'hello' and end with 'hello'
re = /h.llo/i; //Matches any ONE charactor
re = /h*llo/i; //Matches any character 0 or more times
re = /gre?a?y/i; //Optional character gry也匹配
re = /gre?a?y\?/i; //Escap character \
Brackets[]
(1)【ab】代表匹配a或b;
(2)【a-z】代表匹配小写字母a-z;
(3)【^ab】代表匹配除了a和b的任意字符;
//Brackets [] - Character Sets
re = /gr[ae]y/i; //可以匹配[]中的任何一个 //Must be an a or e //不能为空
re = /[GF]ray/; //Must be G or F
re = /[^GF]ray/; //Match anything except a G or F//若为startwith 放在[]外
re = /[A-Z]ray/; //Match any uppercase letter
re = /[a-z]ray/; //Match any lowercase letter
re = /[A-Za-z]ray/; //Match any letter
re = /[0-9]ray/; //Match any digit
re = /[0-9][0-9]ray/;
Braces{}
设定次数
(1)o{3}代表匹配3次o;
(2)o{3,5}代表匹配3-5次o;
(3)o{3,} 代表匹配3次及以上o;
//Braces {} - Quantifiers
re = /Hel{2}o/i; //2个l //Must occur exactly {m} amount of times
re = /Hel{2,4}o/i; //2-4个l
re = /Hel{2,}o/i; //Must occur at least {m} times
Parentheses()
将符号括起来,变为整体;
//Parentheses () - Grouping
re = /([0-9]x){3}$/; //
本文深入解析了正则表达式的各种元字符、括号、方括号、花括号及圆括号的使用方法,包括如何匹配特定字符、任意字符、指定范围内的字符以及重复次数等,帮助读者掌握正则表达式的高级应用。

被折叠的 条评论
为什么被折叠?



