1
00:00:00,870 --> 00:00:06,270
All right guys so in the last video we looked at all the different functions that we can use to evaluate

2
00:00:06,270 --> 00:00:11,960
regular expressions but we didn't really look at some of the you know patterns in some of the Medak

3
00:00:11,960 --> 00:00:14,990
characters and symbols we can use things like that.

4
00:00:15,000 --> 00:00:19,710
So that's what we're going to do in this video but before we want to do that I want to create a custom

5
00:00:19,710 --> 00:00:30,180
function that will basically console log and say whatever we put in contains the regular expression

6
00:00:30,240 --> 00:00:34,830
or I'm sorry the regular expression contains whatever we put in or does not contain whatever we put

7
00:00:34,830 --> 00:00:35,710
in.

8
00:00:35,710 --> 00:00:38,610
I'm just a cleaner way of handling the result.

9
00:00:38,640 --> 00:00:45,260
OK so let's go ahead and create a function and we'll just call this our e test for regular expression

10
00:00:45,270 --> 00:00:46,800
test.

11
00:00:46,840 --> 00:00:47,320
All right.

12
00:00:47,340 --> 00:00:49,790
And we're actually going to use the test method.

13
00:00:49,830 --> 00:00:57,000
We're going to say if and actually this is going to take in two things the regular expression and the

14
00:00:57,000 --> 00:01:09,120
string and we're going to say if our e door test wrapped the string then we want to console dot log

15
00:01:10,500 --> 00:01:14,310
and we're going to put in some tactics here and we're going to say that the string

16
00:01:16,740 --> 00:01:28,380
matches the rebought source OK because the rebought source will keep out the the slashes and just give

17
00:01:28,380 --> 00:01:35,210
us the expression else then what we want to do we want to console log.

18
00:01:35,940 --> 00:01:48,390
I was just grab this OK and this time we're going to say does not match the regular expression.

19
00:01:48,390 --> 00:01:50,680
This should actually be matches.

20
00:01:50,760 --> 00:01:52,140
All right.

21
00:01:52,140 --> 00:01:56,070
And then I also want to do exact as well.

22
00:01:56,250 --> 00:01:56,970
Right above it.

23
00:01:56,970 --> 00:02:03,200
So it's just I'm just to just to give us another type of output.

24
00:02:03,210 --> 00:02:10,670
So we'll say Konst Konst as TR.

25
00:02:10,680 --> 00:02:17,880
This is what will actually be editing the string we'll be editing and then we'll also do Consta result

26
00:02:18,540 --> 00:02:27,090
equals Ari dog exec and then pass in the string and then log the result.

27
00:02:28,330 --> 00:02:33,140
See a console log result.

28
00:02:33,150 --> 00:02:33,440
All right.

29
00:02:33,440 --> 00:02:44,880
So we'll get to logs let's put a comment here also a string to match and then this will be

30
00:02:47,790 --> 00:02:49,920
Lague results.

31
00:02:49,950 --> 00:02:50,260
All right.

32
00:02:50,280 --> 00:03:01,120
And we just finally want to call this function retest and pass an r e to and destroying.

33
00:03:01,170 --> 00:03:02,070
All right.

34
00:03:02,070 --> 00:03:06,450
So now we're going to go up above here and this is where we're going to start to create our regular

35
00:03:06,450 --> 00:03:15,060
expressions so let's say let R E and then I'm just going to keep going down and and redefining r e so

36
00:03:15,060 --> 00:03:16,940
we can do our tests.

37
00:03:16,980 --> 00:03:17,310
All right.

38
00:03:17,310 --> 00:03:22,980
So we already talked about literal characters.

39
00:03:22,980 --> 00:03:23,240
All right.

40
00:03:23,250 --> 00:03:30,540
So if we were to say R E equals and then put our slashes in to represent an expression literal and say

41
00:03:30,570 --> 00:03:40,320
hello and then and here we'll say hello world and save and you can say you can see no we're getting

42
00:03:40,320 --> 00:03:42,780
no from this part from the exam.

43
00:03:42,810 --> 00:03:50,700
And then we're getting hello world which is the string does not match hello which is the expression.

44
00:03:50,700 --> 00:03:57,770
And the reason it doesn't match is because we don't have the case the case sensitive flag.

45
00:03:57,840 --> 00:04:07,530
So if we were to add the I hear and save now it will match ok because it's case insensitive so you can

46
00:04:07,530 --> 00:04:14,190
see we're going to get the result array from the exec and then we're also going to get our little custom

47
00:04:14,190 --> 00:04:17,490
message from our custom retests function.

48
00:04:17,490 --> 00:04:19,610
All right so that's how this is going to work.

49
00:04:19,620 --> 00:04:23,580
Now let's step away from just the literal characters.

50
00:04:23,970 --> 00:04:28,620
Well we're going to use literal characters along with Medich characters symbols.

51
00:04:28,620 --> 00:04:34,560
All right so let's say Metta character symbols and there's different types of Medak characters.

52
00:04:34,560 --> 00:04:38,020
These are just these are symbols that we can use.

53
00:04:38,070 --> 00:04:47,650
So let's say our equals and let's look at the carrot symbols so this is the six shift six character

54
00:04:48,030 --> 00:04:50,770
and then we're just going to put an H here.

55
00:04:51,090 --> 00:04:54,680
And let's also use the I flag.

56
00:04:54,690 --> 00:05:02,650
All right so what this means what this does is it means must start with.

57
00:05:03,000 --> 00:05:07,820
OK so if we go Henry run this we're going to get basically a true k.

58
00:05:07,830 --> 00:05:14,160
Hello matches this expression because it's saying that whatever this is the string and has to start

59
00:05:14,160 --> 00:05:18,600
with an H and it can be case insensitive.

60
00:05:18,600 --> 00:05:21,510
If I were to remove that I then we're going to get now.

61
00:05:21,570 --> 00:05:23,590
And it does not match.

62
00:05:23,610 --> 00:05:26,990
All right but I'm going to keep the case insensitive flag.

63
00:05:27,000 --> 00:05:30,860
Now if I were to put Brad hello world and say we're going to get.

64
00:05:30,880 --> 00:05:36,040
No we're not we're not going to get a match because it's not starting with the letter H.

65
00:05:36,060 --> 00:05:42,480
Ok if I were to put an H here H rat or whatever that will work because it's starting with the letter

66
00:05:42,540 --> 00:05:42,980
H.

67
00:05:42,990 --> 00:05:50,270
Ok so just remember this symbol means start with OK and at the same time we have ends with.

68
00:05:50,760 --> 00:05:53,250
So Samos ends with.

69
00:05:53,250 --> 00:05:55,790
And that's the money sign.

70
00:05:55,800 --> 00:05:56,230
All right.

71
00:05:56,240 --> 00:05:59,080
Now this string hello world ends with a D.

72
00:05:59,280 --> 00:06:03,030
So let's go ahead and put a money sign after it.

73
00:06:03,030 --> 00:06:10,290
So this is saying it must end with a D save and run and we get ok it matches you can see the indexes

74
00:06:10,350 --> 00:06:13,440
at 10 which is right here.

75
00:06:13,470 --> 00:06:16,280
Now it doesn't have to be just one character.

76
00:06:16,290 --> 00:06:19,720
We could do the word world.

77
00:06:20,430 --> 00:06:24,570
And that would match because it is in fact ending with the word world.

78
00:06:24,600 --> 00:06:29,090
We could even do a lower case because we have the the case insensitive flag.

79
00:06:29,520 --> 00:06:39,060
OK but if we did like f world and say that's not going to work if we did space world that will work

80
00:06:39,330 --> 00:06:45,180
because we're basically going from here over and space world matches.

81
00:06:45,180 --> 00:06:45,480
All right.

82
00:06:45,480 --> 00:06:46,890
So hopefully that makes sense.

83
00:06:46,890 --> 00:06:47,480
So.

84
00:06:47,640 --> 00:06:50,440
So carrot is start money signed.

85
00:06:50,460 --> 00:06:56,820
And just remember that right now you can also do begins with and ends with.

86
00:06:56,820 --> 00:06:57,990
So if we were to say

87
00:07:01,860 --> 00:07:09,950
let's say hello like that and then let's put the money sign at the end and the carrot sign at the beginning.

88
00:07:10,140 --> 00:07:15,830
That means that even if we have hello world here if we save we're going to get a not a match.

89
00:07:15,850 --> 00:07:16,440
Hey we're going to get.

90
00:07:16,440 --> 00:07:23,910
No because what this is saying is that it can only be how low it has to start with hello and with hello

91
00:07:23,910 --> 00:07:28,560
So if I were to get rid of this and save that we'll match.

92
00:07:28,560 --> 00:07:42,710
All right so put a comment here and will say must begin and end with.

93
00:07:43,110 --> 00:07:50,190
All right so the next thing we'll look at is the dawn of the period character and this means that it

94
00:07:50,190 --> 00:07:52,140
can match any one character.

95
00:07:52,140 --> 00:08:02,220
So what I'm going to do is just copy this and let's replace the e with a dot.

96
00:08:02,400 --> 00:08:13,050
All right so this will say matches any one character.

97
00:08:13,110 --> 00:08:17,060
So if we go ahead and save this and run this will pass.

98
00:08:17,100 --> 00:08:21,780
Now this will also pass if I put in let's say R and save.

99
00:08:21,780 --> 00:08:22,620
That'll pass.

100
00:08:22,620 --> 00:08:24,500
If I put in the old one.

101
00:08:24,660 --> 00:08:29,070
That'll pass if I put in a symbol that will pass anything here.

102
00:08:29,070 --> 00:08:31,470
I can put in place of this dot.

103
00:08:31,470 --> 00:08:33,720
It's basically like a wild card.

104
00:08:33,720 --> 00:08:39,180
Now the difference between this and the asterisk which will look at next which is the actual wildcard

105
00:08:39,210 --> 00:08:46,680
is it has to be one character for instance I couldn't say you know e e like that.

106
00:08:46,680 --> 00:08:50,610
If I do that it's not going to match because this is two extra characters.

107
00:08:50,610 --> 00:08:52,520
This defines one.

108
00:08:52,620 --> 00:08:52,940
All right.

109
00:08:52,940 --> 00:08:55,110
Now let's try it with the asterisk

110
00:08:58,030 --> 00:09:08,360
if I were to do that what this does is it matches any character zero

111
00:09:11,230 --> 00:09:12,920
or more times.

112
00:09:13,130 --> 00:09:13,530
OK.

113
00:09:13,540 --> 00:09:23,260
So if I go ahead and run that see what work I want to remove or move the start and finish from both

114
00:09:23,260 --> 00:09:24,680
of these.

115
00:09:25,480 --> 00:09:25,780
OK.

116
00:09:25,780 --> 00:09:28,560
So we save that and that works.

117
00:09:28,600 --> 00:09:35,020
And the reason it's working is because the actress represents any character zero or more times so zero

118
00:09:35,020 --> 00:09:39,440
we don't even need that value there so I could remove it completely.

119
00:09:39,610 --> 00:09:41,580
And that still matches.

120
00:09:41,590 --> 00:09:41,950
All right.

121
00:09:41,950 --> 00:09:46,690
So that's a complete wildcard including nothing at all.

122
00:09:46,690 --> 00:09:52,090
All right so now let's take a look at the question mark which is an optional character.

123
00:09:52,090 --> 00:09:57,850
So what am I going to do is give you an example here of something you may you might actually use and

124
00:09:57,850 --> 00:09:59,420
that is the word gray.

125
00:09:59,440 --> 00:10:06,170
OK so when we have the word gray sometimes it's spelled with an A Sometimes it's spelt with an e.

126
00:10:06,340 --> 00:10:12,470
So what I want to do is have an optional carrot an optional a or an optional E..

127
00:10:12,670 --> 00:10:18,240
And the way that you define that is by putting a question mark after the character that's optional.

128
00:10:18,310 --> 00:10:25,590
So we're going to say it is optional and is optional and then a is optional.

129
00:10:26,690 --> 00:10:27,040
Right.

130
00:10:27,040 --> 00:10:36,120
So let's go over here and just say optional character and then down here let's put in gray.

131
00:10:36,130 --> 00:10:40,010
See if that matches it does ok we get our response back.

132
00:10:40,090 --> 00:10:46,270
And if I put an e here and save that matches if I put in I in save we get.

133
00:10:46,270 --> 00:10:47,150
No.

134
00:10:47,530 --> 00:10:47,960
All right.

135
00:10:47,980 --> 00:10:52,770
Now since both of them are optional I can also just do G.R..

136
00:10:52,820 --> 00:10:53,470
Why.

137
00:10:53,470 --> 00:10:57,620
And that will still work OK because they're both optional.

138
00:10:58,480 --> 00:10:58,790
All right.

139
00:10:58,810 --> 00:11:01,660
I'm going to show you another way of doing this as well.

140
00:11:01,660 --> 00:11:06,180
But for now the next thing I want to show you is escapin characters.

141
00:11:06,460 --> 00:11:06,800
All right.

142
00:11:06,800 --> 00:11:16,950
So actually let me just bring that down I'll say escape characters.

143
00:11:17,230 --> 00:11:23,650
Now what if we wanted this to be gray with an actual question mark in the question mark being used as

144
00:11:23,650 --> 00:11:24,510
a literal.

145
00:11:24,820 --> 00:11:32,050
So to escape something just like you would with you know a regular javascript's string you want to use

146
00:11:32,050 --> 00:11:34,820
you or your backslash.

147
00:11:34,840 --> 00:11:37,910
OK so let's put a question mark here.

148
00:11:37,930 --> 00:11:42,190
Now if we leave it this way it's basically saying that y is optional.

149
00:11:42,430 --> 00:11:50,500
So if I save this this will work but it's basically saying Y is optional so I can put like a 4 here

150
00:11:50,500 --> 00:11:51,120
if I want.

151
00:11:51,130 --> 00:11:52,690
And that will still work.

152
00:11:52,690 --> 00:11:54,810
And that's not what I want it for I want it to be.

153
00:11:54,820 --> 00:11:57,220
I want the questionmark to be a literal.

154
00:11:57,340 --> 00:12:02,530
So I'm going to prefix it with a backslash.

155
00:12:02,800 --> 00:12:09,760
So now I'll save and it works it's a match and if I try to change the Y to like are we get.

156
00:12:09,760 --> 00:12:10,280
No.

157
00:12:10,330 --> 00:12:12,270
Because it's no longer optional.

158
00:12:12,280 --> 00:12:15,400
The question mark is just being taken as a literal.

159
00:12:15,730 --> 00:12:19,050
All right so these are some of the meta characters symbols now in the next video.

160
00:12:19,050 --> 00:12:24,820
I'm going to continue with this same file and we're going to take a look at using brackets which are

161
00:12:24,820 --> 00:12:31,600
used for character sets as well as braces curly braces which are quantifiers and then parentheses which

162
00:12:31,600 --> 00:12:32,850
are used for grouping.

163
00:12:33,040 --> 00:12:33,300
All right.

164
00:12:33,310 --> 00:12:35,280
So I will see you in the next lecture.

