JS_正则表达式基础学习


链接: RegexOne.

1.Lesson 1: An Introduction, and the ABCs

Exercise 1: Matching Characters
Task Text
Match abcdefg
Match abcde
Match abc

^{a-c}\w*$
/*
^表示开始,$表示结束
abc表示匹配字符串前缀
\w*中表示匹配字母,*表示出现0次或者多次。
*/

2.Lesson 1½: The 123s

Exercise 1½: Matching Digits
Task Text
Match abc123xyz
Match define “123”
Match var g = 123;

^\w+\s?.+$
/*
\w+表示匹配一个或多个字母
\s?表示匹配0个或1个空格
.+表示匹配1个或多个任意字符
*/

3.Lesson 2: The Dot

Exercise 2: Matching With Wildcards
Task Text
Match cat.
Match 896.
Match ?=+.
Skip abc1

^.+[^abc1]$
/*
.+表示匹配1个或多个任意字符
[^abc1]抛除abc1字符
*/

4.Lesson 3: Matching specific characters

Exercise 3: Matching Characters
Task Text
Match can
Match man
Match fan
Skip dan
Skip ran
Skip pan

^[cmf]an$
/*
[cmf]匹配字符串含c,m,f的前缀
an是后缀
*/

5.Lesson 4: Excluding specific characters

Exercise 4: Excluding Characters
Task Text
Match hog
Match dog
Skip bog

^[^b]og$
/*
[^b]匹配字符串不含b的前缀
og是后缀
*/

6.Lesson 5: Character ranges

Exercise 5: Matching Character Ranges
Task Text
Match Ana
Match Bob
Match Cpc
Skip aax
Skip bby
Skip ccz

^waz{3,5}up$
/*
z{3,5}中的z匹配3-5次
*/

7.Lesson 7: Mr. Kleene, Mr. Kleene

Exercise 7: Matching Repeated Characters
Task Text
Match aaaabcc
Match aabbbbc
Match aacc
Skip a

^(aa)+b*c+$
/*
(aa)+,匹配1个或多个aa
b*出现0个或多个b
c+出现1个或多个c
*/

8.Lesson 8: Characters optional

Exercise 8: Matching Optional Characters
Task Text
Match 1 file found?
Match 2 files found?
Match 24 files found?
Skip No files found.

^\d+\s(\w+\s)+\w+\?$
/*
\d+匹配一个或多个数字
\s匹配一个空格
(\w+\s)+匹配一个或多个(字符串+空格)
\?匹配?
*/

9.Lesson 9: All this whitespace

Exercise 9: Matching Whitespaces
Task Text
Match 1. abc
Match 2.     abc
Match 3.         abc
Skip 4.abc

^\d\.\s+abc$
/*
\d匹配一个数字
\.匹配.
\s+匹配一个或多个空格
abc匹配abc
*/

10.Lesson 10: Starting and ending

Exercise 10: Matching Lines
Task Text
Match Mission: successful
Skip Last Mission: unsuccessful
Skip Next Mission: successful upon capture of target

^\w+\:\ssuccessful$
/*
\w+匹配字符串
\:匹配:
\s匹配一个空格
abc匹配abc
*/

11.Lesson 11: Match groups

Exercise 11: Matching Groups
Task      Text      Capture Groups
Capture    file_record_transcript.pdf      file_record_transcript
Capture   file_07241999.pdf      file_07241999
Skip   testfile_fake.pdf.tmp

^([\w+_?\d*]+)\.pdf$
/*
[\w+_?\d*]+匹配字符串、下划线、数字串一个或多个
\.pdf匹配.pdf
*/

12.Lesson 12: Nested groups

Exercise 12: Matching Nested Groups
Task     Text    Capture   Groups
Capture   Jan 1987    Jan 1987 1987
Capture      May 1969    May 1969 1969
Capture      Aug 2011    Aug 2011 2011

^(\w+\s+(\d+))$
/*
\w+\s+(\d+) 字符-空格-数字(1个或多个数字)
*/

13.Lesson 13: More group work

Exercise 13: Matching Nested Groups
Task    Text    Capture Groups
Capture    1280x720     1280 720
Capture    1920x1600    1920 1600
Capture    1024x768     1024 768

^(\d{3,4})[x|\s](\d{3,4})$
/*
(\d{3,4})匹配3-4个数字
[x|\s]匹配空格或x
*/

14.Lesson 14: It’s all conditional

Exercise 14: Matching Conditional Text
Task Text
Match I love cats
Match I love dogs
Skip I love logs
Skip I love cogs

^\w\s\w+\s(cats|dogs)$
/*
(cats|dogs)匹配cats或dogs
*/

15.Lesson 15: Other special characters

Exercise 15: Matching Other Special Characters
Task     Text
Match    The quick brown fox jumps over the lazy dog.
Match    There were 614 instances of students getting 90.0% or above.
Match    The FCC had to censor the network for saying &$#*@!.

^(\w+)\s(\w+)(\d)*.+$
/*
(\w+)\s(\w+)(\d)* 匹配字符、空格、字符、数字
.+匹配多个任意字符
*/

御剑江湖载酒行,江湖邋遢不知名。笑听风雨身后事,与君携手发狂吟。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值