JS Regular Expression: Metacharacter Symbols+ Brackets + Braces + Parentheses

本文深入解析了正则表达式的各种元字符、括号、方括号、花括号及圆括号的使用方法,包括如何匹配特定字符、任意字符、指定范围内的字符以及重复次数等,帮助读者掌握正则表达式的高级应用。

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}$/;   //

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值