1
00:00:00,690 --> 00:00:06,390
Hey, and welcome to the regex lesson, and in this lesson, we're going to try to cover off at least

2
00:00:06,390 --> 00:00:12,030
a little bit of regex, there's a whole lot that can be done with regex and there's a lot of different

3
00:00:12,030 --> 00:00:13,710
situations to use regex.

4
00:00:13,890 --> 00:00:21,450
So within JavaScript, you can check to see if the characters match a certain pattern and utilize that

5
00:00:21,450 --> 00:00:22,380
within your code.

6
00:00:22,590 --> 00:00:28,620
And regex is a great way to create patterns that you can look for within strings.

7
00:00:28,930 --> 00:00:35,570
So you've got an example here of regular expressions on regex, of course, is short for regular expressions.

8
00:00:35,910 --> 00:00:39,540
So it creates a regular expression object for matching text with the pattern.

9
00:00:39,840 --> 00:00:45,270
So essentially what you do is you create a pattern and then you look for within a string, for a match,

10
00:00:45,270 --> 00:00:46,080
within the pattern.

11
00:00:46,080 --> 00:00:50,730
So regex is creating a pattern and then checking to see if it matches.

12
00:00:51,030 --> 00:00:57,690
There's a whole bunch of different combinations that you can do in order to fine-tune your pattern that

13
00:00:57,690 --> 00:00:59,490
you're looking for a bunch of different flags.

14
00:00:59,790 --> 00:01:05,220
So global match, ignoring case, multiline dot, all Unicode sticky.

15
00:01:05,450 --> 00:01:06,630
So there's a lot you can do.

16
00:01:06,630 --> 00:01:12,030
And as I said, there is really we're just going to scratch the surface in this lesson because regex

17
00:01:12,030 --> 00:01:17,620
can get fairly complex writing out these patterns and looking for different matches within the patterns.

18
00:01:18,150 --> 00:01:22,890
There is also a really nice website that can help you generate the patterns.

19
00:01:23,040 --> 00:01:29,400
And as you can see, we've got the expression example here, and it's looking to fight and match words

20
00:01:29,550 --> 00:01:30,920
that are matching the pattern.

21
00:01:31,170 --> 00:01:38,640
So it's looking for any uppercase letters and it's taking the entire word that matches and returning

22
00:01:38,640 --> 00:01:41,640
back the words that have started with uppercase.

23
00:01:41,910 --> 00:01:47,820
You can change this as well to A, to Z lowercase, and you're going to see that it's returning back

24
00:01:47,820 --> 00:01:50,520
all that A to Z words with the lowercase.

25
00:01:50,520 --> 00:01:53,510
So exception, there's no uppercase letters being done.

26
00:01:53,730 --> 00:01:56,460
There's a lot of different types of patterns that you can save here.

27
00:01:56,640 --> 00:02:02,070
And the cheat sheet is also super useful because it gives you some really great information on how to

28
00:02:02,070 --> 00:02:02,900
create the patterns.

29
00:02:03,300 --> 00:02:06,900
So I don't spend too much time on the patterns because it does get fairly lengthy.

30
00:02:07,080 --> 00:02:12,030
But I'll show you once you do design the pattern that you're looking for, how you can utilize it with

31
00:02:12,030 --> 00:02:15,810
a JavaScript and pull out information from your strings.

32
00:02:16,200 --> 00:02:19,370
So first, we need to have a string to work with.

33
00:02:19,530 --> 00:02:24,390
So I'm going to just create a string with some random words and characters in here.

34
00:02:25,080 --> 00:02:30,420
And this is going to be similar to the one that we used when we're using the string methods and I'm

35
00:02:30,420 --> 00:02:31,930
going to have JavaScript twice.

36
00:02:31,950 --> 00:02:34,670
So let's create our first expression.

37
00:02:34,890 --> 00:02:40,860
So using the regular expression website in order to generate our expression.

38
00:02:41,040 --> 00:02:41,900
And then we're looking.

39
00:02:41,940 --> 00:02:45,510
The pattern that we're looking for is any of the spaces between the words.

40
00:02:47,310 --> 00:02:50,980
And then looking for another word that's going to be right after that.

41
00:02:51,300 --> 00:02:55,820
So within the tools, we've got an explanation of what's going on.

42
00:02:55,830 --> 00:02:57,680
So that's why this is such a great resource.

43
00:02:57,690 --> 00:03:01,950
That's regex, our dotcom where we're capturing matches.

44
00:03:01,950 --> 00:03:02,900
Any word?

45
00:03:03,210 --> 00:03:07,260
And then next, the word needs to have a whitespace character.

46
00:03:07,270 --> 00:03:09,390
So Space's Tab's line breaks.

47
00:03:09,630 --> 00:03:13,140
And then also we're matching any word character.

48
00:03:13,160 --> 00:03:16,780
So alphanumeric and underscore is all part of a word.

49
00:03:17,070 --> 00:03:22,380
So that's why we get these sets of the two words together as the matches.

50
00:03:22,620 --> 00:03:26,170
But this one here isn't matching because it's got a dot.

51
00:03:26,700 --> 00:03:28,190
This one's got an ampersand.

52
00:03:28,440 --> 00:03:30,000
This one's got a dot as well.

53
00:03:30,120 --> 00:03:32,760
So these ones aren't matching the pattern that we're looking for.

54
00:03:32,790 --> 00:03:38,070
So this is the particular pattern that we're looking for and this is the regular expression that we're

55
00:03:38,070 --> 00:03:38,670
going to look for.

56
00:03:39,800 --> 00:03:44,840
So using the string method of replace, we've got our pattern that we're looking for.

57
00:03:45,620 --> 00:03:52,370
So whereas before you might have been used to using replace if you want to replace, hello, and then

58
00:03:52,370 --> 00:03:59,450
we specify how we want to replace it with what text we want to replace it, and then we can output temp

59
00:03:59,450 --> 00:04:00,650
one into the console.

60
00:04:00,920 --> 00:04:06,470
So you can see now I've replaced the word, but when we bring in regular expressions, we get a little

61
00:04:06,470 --> 00:04:07,620
bit more power to it.

62
00:04:08,090 --> 00:04:14,790
So instead of just doing a regular replace, if we specify the pattern that we want to match.

63
00:04:14,790 --> 00:04:16,940
So it's that regular expression pattern.

64
00:04:17,420 --> 00:04:20,950
Remember, we're matching the two words, hello world.

65
00:04:20,960 --> 00:04:28,460
If we want to replace both those, we could say by people, and that would replace the Hello world.

66
00:04:29,470 --> 00:04:36,130
As it matches the pattern that we're indicating, so essentially it's taking the first two words that

67
00:04:36,130 --> 00:04:41,860
match the pattern and it's updating them with whatever value we're adding into the string.

68
00:04:42,940 --> 00:04:45,850
Let's try a few more so we can console logout.

69
00:04:46,900 --> 00:04:51,660
And taking that same string, we're going to look for a match in the string.

70
00:04:52,900 --> 00:04:55,580
And what we're going to look for is a regular expression.

71
00:04:55,600 --> 00:04:58,110
So we're going to look for any matches that match.

72
00:04:58,370 --> 00:05:03,510
So this is an uppercase G and we've got some information on this particular G.

73
00:05:03,520 --> 00:05:06,370
So we've got the letter that we're looking for, says J.

74
00:05:06,730 --> 00:05:08,040
And matching the pattern.

75
00:05:08,050 --> 00:05:13,560
We've got an index value of 12 sets where it's first located and then we've got the entire input.

76
00:05:13,570 --> 00:05:15,060
So that's the string that we're working with.

77
00:05:15,430 --> 00:05:23,470
If we change this and add some flags to it, so we add GI to it as flags, you can see what gets returned

78
00:05:23,470 --> 00:05:29,440
back is we're returning back all the instance of it, wherever there's G and then as well, this isn't

79
00:05:29,440 --> 00:05:31,220
going to be case sensitive either.

80
00:05:31,600 --> 00:05:34,810
So notice that we're returning back all of the JS.

81
00:05:35,080 --> 00:05:37,860
So all the instances of G get returned back.

82
00:05:37,870 --> 00:05:43,270
And this is a more usable array format where we can loop through all these instances and we also have

83
00:05:43,270 --> 00:05:43,870
a length there.

84
00:05:43,870 --> 00:05:49,030
So that means that we can use a loop in order to iterate through all the and there's a number of other

85
00:05:49,030 --> 00:05:50,410
things that we can do as well.

86
00:05:50,420 --> 00:05:54,790
So within console log so we can pass in our regular expression.

87
00:05:54,820 --> 00:05:59,560
In this case, I'm going to use JavaScript again and I'm going to use test.

88
00:05:59,770 --> 00:06:04,180
So it executes a search for the match between the regular expression and the specified string.

89
00:06:04,480 --> 00:06:06,780
So the regular expression is JavaScript.

90
00:06:07,060 --> 00:06:13,080
So we're testing to see if JavaScript is available within the string and we get back.

91
00:06:13,090 --> 00:06:13,490
True.

92
00:06:13,690 --> 00:06:19,990
So this is returning back a boolean value and we can also check to see if there's any digits.

93
00:06:20,440 --> 00:06:21,430
So any digits.

94
00:06:21,430 --> 00:06:29,020
029 using the regular expression and we can see that there are digits in here if I remove those and

95
00:06:29,020 --> 00:06:33,790
I still get true because I still have a few digits there and once I remove out all of the digits, I

96
00:06:33,790 --> 00:06:34,360
get false.

97
00:06:35,740 --> 00:06:37,120
Let me add them back.

98
00:06:37,750 --> 00:06:43,930
Here's another one that we can try, so there's execute as well and we put our regular expressions.

99
00:06:43,930 --> 00:06:50,470
So even if you place it within a string format like this or a variable, if you if you place it within

100
00:06:50,470 --> 00:06:53,200
a variable or if you write it out directly, same thing.

101
00:06:53,290 --> 00:06:59,370
And then passing in the string that we're using, we see that we're looking for all of the digits.

102
00:06:59,680 --> 00:07:01,510
So we're turning back all of the digits there.

103
00:07:01,870 --> 00:07:04,070
So you get an index of 23.

104
00:07:04,420 --> 00:07:09,220
So if we move these digits, if we have digits here earlier, that's what's going to get returned back

105
00:07:09,430 --> 00:07:11,590
to looking for the first instance of that.

106
00:07:11,740 --> 00:07:12,680
Let's try an array.

107
00:07:12,790 --> 00:07:19,180
So we have a simple array and we'll take doddery and we'll turn into a string and spacing it out with

108
00:07:19,180 --> 00:07:19,690
the dots.

109
00:07:19,690 --> 00:07:20,660
You can do that as well.

110
00:07:20,680 --> 00:07:25,930
So just call this temp for and let's join it and clear the console and temp three.

111
00:07:26,110 --> 00:07:31,030
So now you see that we've got a string with the dots in the middle and this way you can use the regular

112
00:07:31,030 --> 00:07:32,800
expressions with the strings.

113
00:07:33,130 --> 00:07:37,440
If I want to find all the instances of two, I can search and I can return that.

114
00:07:37,450 --> 00:07:40,700
So it executes a pattern on a regular expression.

115
00:07:40,870 --> 00:07:44,110
So whatever word you want to include and then returning that back.

116
00:07:44,230 --> 00:07:49,990
And as you can see, the search is returning back the value of two, and it's seeing that within index

117
00:07:49,990 --> 00:07:50,870
value of seven.

118
00:07:51,040 --> 00:07:57,880
And the difference between search and match is that if we do it within the same thing here, if we do,

119
00:07:58,060 --> 00:08:00,070
we see we get the match being returned back.

120
00:08:00,080 --> 00:08:01,630
So it does have that first match.

121
00:08:01,840 --> 00:08:07,660
And then again, just as we did up here, you can specify to return back all the instances of it and

122
00:08:07,660 --> 00:08:09,160
to not be case sensitive.

123
00:08:09,460 --> 00:08:11,520
So that returns it back with an array format.

124
00:08:11,620 --> 00:08:13,820
And then, of course, we can utilize that within our code.

125
00:08:13,840 --> 00:08:15,870
So I know there's been quite a bit that we've gone through.

126
00:08:15,880 --> 00:08:20,800
So I do encourage you to if you want to read up more about it, you can always check out the Mozilla

127
00:08:20,800 --> 00:08:27,070
Developer Network or regex or dot com, which is an excellent resource, and we'll give you more information

128
00:08:27,070 --> 00:08:29,080
about your regular expressions as you create them.

129
00:08:29,380 --> 00:08:32,590
And you could also I've included a little bit of a cheat sheet here as well.

130
00:08:32,830 --> 00:08:37,540
That gives you some of the more common values that are used, as well as the flags that you can use

131
00:08:37,660 --> 00:08:39,430
in order to create your regular expressions.

132
00:08:39,580 --> 00:08:45,820
So, of course, we do have a challenge for this lesson, and that's to get all of the email addresses

133
00:08:45,970 --> 00:08:47,770
out of this string.

134
00:08:47,770 --> 00:08:49,360
So we've got two email addresses.

135
00:08:49,480 --> 00:08:50,700
You can see them visually.

136
00:08:50,830 --> 00:08:53,830
How do you get them out with code and put them into the console?

137
00:08:54,370 --> 00:08:55,270
So that's the challenge.

138
00:08:55,510 --> 00:08:59,910
You can take a look at this code and we'll walk you through the solution coming up.

139
00:09:00,070 --> 00:09:02,950
And this is the pattern, and I know it's a really long pattern.

140
00:09:03,100 --> 00:09:08,120
So what is essentially doing is looking for all the characters, A to Z, uppercase, lowercase, eight

141
00:09:08,120 --> 00:09:09,880
to zero zero to nine.

142
00:09:10,000 --> 00:09:17,290
So any digits as well as dots underscores and dashes looking for them all the way up to eight and then

143
00:09:17,290 --> 00:09:24,070
again matching that pattern, looking for a dot, separated with a dot and then again matching that

144
00:09:24,070 --> 00:09:24,550
pattern.

145
00:09:24,580 --> 00:09:31,330
So this is for whatever the email address is, some text, that symbol, some more text, another word

146
00:09:31,810 --> 00:09:34,590
dot, and then another word, a set of characters.

147
00:09:35,130 --> 00:09:36,190
So that's what I'm looking for.

148
00:09:36,340 --> 00:09:39,430
Finds all of the matches using match within an array.

149
00:09:39,550 --> 00:09:42,690
And then we loop through the array and output the values into the console, the positive.

150
00:09:42,760 --> 00:09:43,840
We'll try this one out.

151
00:09:43,930 --> 00:09:46,360
It's going into the regex website.

152
00:09:46,660 --> 00:09:51,730
You can see we create the pattern to match looking for all the emails and it highlights them in blue,

153
00:09:51,910 --> 00:09:52,660
checking to see.

154
00:09:52,810 --> 00:09:56,930
And there's also a better explanation here of what we're doing and what we're searching for.

155
00:09:57,220 --> 00:10:01,260
So all the different values that we're looking for and you can see that the emails are coming out.

156
00:10:01,270 --> 00:10:06,790
So if we take the string and if we duplicate the string, you can see it's still grabbing all of the

157
00:10:06,790 --> 00:10:08,980
emails and that's exactly what we want to do.

158
00:10:09,260 --> 00:10:13,320
So let's open up our console and we're going to copy and paste this in.

159
00:10:13,480 --> 00:10:19,540
So update on the string value that we're going to use the string with the emails and let's create our

160
00:10:19,540 --> 00:10:20,460
regular expressions.

161
00:10:20,460 --> 00:10:22,430
So that will be a regular expression, too.

162
00:10:22,500 --> 00:10:28,030
So going back to the website and copy that expression and let's add that in and looking for, gee,

163
00:10:28,210 --> 00:10:33,550
that's a regular expression for the emails that we get our email data within a variable.

164
00:10:33,650 --> 00:10:43,550
So taking the my string to string and then looking for a match and matching regular expression to that,

165
00:10:43,600 --> 00:10:44,940
we can now put it into the console.

166
00:10:45,100 --> 00:10:48,520
So see, we've got all the emails from the string coming in.

167
00:10:49,090 --> 00:10:55,870
Now we can use just a regular Falu, and while X is less than the length of email data and increment

168
00:10:55,870 --> 00:11:00,640
X by what, we can output all of the emails into the console, this format.

169
00:11:00,820 --> 00:11:02,920
So we've got all of them within the email data.

170
00:11:03,190 --> 00:11:07,410
And as we're iterating looping through, you can see output the emails.

171
00:11:07,660 --> 00:11:13,190
So even if we had even more emails here, you can see they're also going to get output into the console.

172
00:11:13,480 --> 00:11:14,850
So now we've got three emails.

173
00:11:14,890 --> 00:11:16,180
They also get output.

174
00:11:16,390 --> 00:11:17,440
So awfully dynamic.

175
00:11:17,830 --> 00:11:22,480
So hope you had an opportunity to try this out and get more familiar with working with regex.
