前端之家收集整理的这篇文章主要介绍了
正则表达式的语法,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_
404_0@
元字符
@H_
404_0@我们使用的字符“[”和“
]”叫元字符,对模式有特殊的效果。这种元字符共有11个,它们扮演着不同的角色。如果想在建立的模式中包含这些元字符中的某一个字符,需要在该字符前使用转义字符“\”。
@H_404_0@元字符 |
@H_404_0@描述 |
@H_404_0@^开始 @H_404_0@(beginning) |
@H_404_0@字符“^”之后的实体(entity),必须在配匹配的字符串开始部分找到。 @H_404_0@例: @H_404_0@^h @H_404_0@能匹配的字符串:hello,h,hh @H_404_0@不能匹配的字符串:character,ssh |
@H_404_0@$结束 @H_404_0@(end) |
@H_404_0@字符“$”之前的实体(entity),必须在配匹配的字符串尾部找到。 @H_404_0@例: @H_404_0@e$ @H_404_0@能匹配的字符串:sample,e,file @H_404_0@不能匹配的字符串:extra,shell |
@H_404_0@.任何字符 @H_404_0@(any) |
@H_404_0@匹配任何字符。 @H_404_0@例: @H_404_0@hell. @H_404_0@能匹配的字符串:hello,hellx,hell5,hell! @H_404_0@不能匹配的字符串:hell,helo |
@H_404_0@[]组 @H_404_0@(set) |
@H_404_0@匹配指定字符集内的任何字符。 @H_404_0@语法:[a-z]表示一个区间,[abcd]表示一组,[a-z0-9]表示两个区间。 @H_404_0@例: @H_404_0@hell[a-y123] @H_404_0@能匹配的字符串:hello,hell1,hell2 @H_404_0@不能匹配的字符串:hellz,hell4,heloo |
@H_404_0@[^]否定组 @H_404_0@(negateset) |
@H_404_0@匹配任何不包括在指定字符集内的字符串 @H_404_0@例: @H_404_0@hell[^a-np-z0-9] @H_404_0@能匹配的字符串:hello,hell @H_404_0@不能匹配的字符串:hella,hell5 |
@H_404_0@|或 @H_404_0@(alternation) |
@H_404_0@匹配符号“|”之前或之后的实体。 @H_404_0@例: @H_404_0@hello|welcome @H_404_0@能匹配的字符串:hello,welcome,helloes,awelcome @H_404_0@不能匹配的字符串:hell,ellow,owelcom |
@H_404_0@()分组 @H_404_0@(grouping) |
@H_404_0@组成一组用于匹配的实体(entity),常用符号“|”协助完成。 @H_404_0@例: @H_404_0@^(hello|hi)there$ @H_404_0@能匹配的字符串:hellothere,hithere @H_404_0@不能匹配的字符串:heythere,ahoythere |
@H_404_0@\转义 @H_404_0@(escape) |
@H_404_0@允许你对一个特殊字符转义(即将具有特殊意义的字符变为普通的文本字符) @H_404_0@例: @H_404_0@hello\. @H_404_0@能匹配的字符串:hello.,hello... @H_404_0@不能匹配的字符串:hello,hella |
@H_
404_0@
量词
@H_
404_0@可以通过使用有限的字符来表达简单的匹配。下表所示的量词允许你扩展实体匹配使用量词可以定制匹配的
次数。
@H_404_0@量词 |
@H_404_0@描述 |
@H_404_0@* @H_404_0@0或多次 |
@H_404_0@字符“*”之前的实体被发现0次或多次。 @H_404_0@例: @H_404_0@he*llo @H_404_0@能匹配的字符串:hllo,hello,heeeello @H_404_0@不能匹配的字符串:hallo,ello |
@H_404_0@+ @H_404_0@1或多次 |
@H_404_0@字符“+”之前的实体被发现1次或多次。 @H_404_0@例: @H_404_0@he+llo @H_404_0@能匹配的字符串:hello,heeello @H_404_0@不能匹配的字符串:hllo,helo |
@H_404_0@? 0或1次 |
@H_404_0@字符“?”之前的实体被发现0次或1次。 @H_404_0@例: @H_404_0@he?llo @H_404_0@能匹配的字符串:hello,hllo @H_404_0@不能匹配的字符串:heello,heeeello |
@H_404_0@{x} @H_404_0@x次 |
@H_404_0@字符“{}”之前的字符必须匹配指定的x次数。 @H_404_0@例: @H_404_0@he{3}llo @H_404_0@能匹配的字符串:heeello,ohheeello @H_404_0@不能匹配的字符串:hello,heello,heeeello |
@H_404_0@{x,} @H_404_0@至少x次 |
@H_404_0@字符“{}”之前的字符必须至少出现x次。(即可以多于x次) @H_404_0@例: @H_404_0@he{3}llo @H_404_0@能匹配的字符串:heeello,heeeeello @H_404_0@不能匹配的字符串:hllo,hello,heello |
@H_404_0@{x,y} @H_404_0@x到y次 |
@H_404_0@字符“{}”之前的字符出现的次数必须介于x和y次之间。 @H_404_0@例: @H_404_0@he{2,4}llo @H_404_0@能匹配的字符串:heello,heeello,heeeello @H_404_0@不能匹配的字符串:hello,heeeeello |
@H_
404_0@
捕获
@H_
404_0@正则表达式机制最后一个
功能是能够捕获子表达式,放在“
()”之间的任何文本,在被捕获后都能用于后面的匹配处理。
@H_404_0@模式 |
@H_404_0@字符串 |
@H_404_0@捕获 |
@H_404_0@^(hello|hi)(sir|mister)$ |
@H_404_0@hellosir |
@H_404_0@$1=hello @H_404_0@$2=sir |
@H_404_0@^(.*)$ |
@H_404_0@Nginxrocks |
@H_404_0@$1=Nginxrocks |
@H_404_0@^(.{1,3})([0-9]{1,4})([?!]{1,2})$ |
@H_404_0@abc1234?! |
@H_404_0@$1=abc @H_404_0@$2=1234 @H_404_0@$3=?! |