前端之家收集整理的这篇文章主要介绍了
正则表达式中容易误解的地方,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
1.
@H_
301_0@ \d Any digit
@H_
301_0@
\D Any character except a digit
@H_
301_0@ \s “whitespace”: space,tab,carriage return,line
Feed,or newline
@H_
301_0@ \S Anything except whitespace
@H_
301_0@ \w A “word character”: [A-Za-z
0-9_]
@H_
301_0@ \W Any character except a word character
@H_
301_0@
@H_
301_0@
@H_
301_0@ 2.
@H_
301_0@ /[.]/ =~ "b"
@H_
301_0@ ֒→ nil
@H_
301_0@
Within brackets,characters like the dot,plus,and star are not special.
@H_
301_0@
@H_
301_0@
@H_
301_0@
@H_
301_0@
@H_
301_0@ 3.
@H_
301_0@ /^[
\[=\]]+$/ =~ '=]=[='
@H_
301_0@ ֒→ 0
@H_
301_0@ To include open and close brackets inside ofbrackets,escape them with a backslash. Thisexpression matches any sequence of one or morecharacters,all of which must be either [,],or =.(The two anchors ensure that there are no characters before or after the matching characters.)
@H_
301_0@
@H_
301_0@
@H_
301_0@ 4.
@H_
301_0@ caret在[] 中的
用法,取反
@H_
301_0@ /[^ab]/ =~ "z"
@H_
301_0@ ֒→ 0
@H_
301_0@ Putting a caret at the beginning of a characterclass causes the set to contain all characters
@H_
301_0@
except the ones listed.
@H_
301_0@
@H_
301_0@
@H_
301_0@ 5.
@H_
301_0@ 正则表达式中间,竖线的
用法:分成两个正则表达式
@H_
301_0@ /^Fine birds
| cows ate\.$/ =~
@H_
301_0@ "Fine birds ate seeds."
@H_
301_0@ ֒→ 0
@H_
301_0@
A vertical bar divides the regular expression intotwo smaller regular expressions. A match means that either the entire left regexp matches or theentire right one does. This regular expression does not mean “Matcheither 'Fine birds ate.' or 'Fine cows ate.'” It actually matches either a string beginning with "Finebirds" or one ending in "cows ate."
原文链接:https://www.f2er.com/regex/358813.html