正则表达式中容易误解的地方

前端之家收集整理的这篇文章主要介绍了正则表达式中容易误解的地方前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
1.
\d Any digit
\D Any character except a digit
\s “whitespace”: space,tab,carriage return,line Feed,or newline
\S Anything except whitespace
\w A “word character”: [A-Za-z 0-9_]
\W Any character except a word character


2.
/[.]/ =~ "b"
֒→ nil
Within brackets,characters like the dot,plus,and star are not special.




3.
/^[ \[=\]]+$/ =~ '=]=[='
֒→ 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.)


4.
caret在[] 中的用法,取反
/[^ab]/ =~ "z"
֒→ 0
Putting a caret at the beginning of a characterclass causes the set to contain all characters
except the ones listed.


5.
正则表达式中间,竖线的用法:分成两个正则表达式
/^Fine birds | cows ate\.$/ =~
"Fine birds ate seeds."
֒→ 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

猜你在找的正则表达式相关文章