AI智能
改变未来

JS_正则表达式基础学习

正则表达式JavaScript版本

  • 1.Lesson 1: An Introduction, and the ABCs
  • 2.Lesson 1½: The 123s
  • 3.Lesson 2: The Dot
  • 4.Lesson 3: Matching specific characters
  • 5.Lesson 4: Excluding specific characters
  • 6.Lesson 5: Character ranges
  • 7.Lesson 7: Mr. Kleene, Mr. Kleene
  • 8.Lesson 8: Characters optional
  • 9.Lesson 9: All this whitespace
  • 10.Lesson 10: Starting and ending
  • 11.Lesson 11: Match groups
  • 12.Lesson 12: Nested groups
  • 13.Lesson 13: More group work
  • 14.Lesson 14: It\’s all conditional
  • 15.Lesson 15: Other special characters

链接: 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个或多个aab*出现0个或多个bc+出现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
TaskTextCapture Groups
Capture1280x720 1280 720
Capture1920x16001920 1600
Capture1024x768 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
MatchThe quick brown fox jumps over the lazy dog.
MatchThere were 614 instances of students getting 90.0% or above.
MatchThe FCC had to censor the network for saying &$#*@!.

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

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

赞(0) 打赏
未经允许不得转载:爱站程序员基地 » JS_正则表达式基础学习