1
00:00:02,200 --> 00:00:09,220
Now to conclude this module, let's have a look at regular expressions. Regular expressions help us work

2
00:00:09,220 --> 00:00:10,760
with strings,

3
00:00:10,780 --> 00:00:16,330
consider this example - you're having an input and there, you get some user input and that should

4
00:00:16,330 --> 00:00:17,410
be an email address,

5
00:00:17,410 --> 00:00:20,140
something like test@test.com,

6
00:00:20,140 --> 00:00:24,850
this is what you get from an input you might have on your page.

7
00:00:24,850 --> 00:00:30,730
Now of course, users can enter anything in there and by accident, they might enter something which is

8
00:00:30,820 --> 00:00:32,540
not a valid email address,

9
00:00:32,590 --> 00:00:36,760
let's say for some reason the @ symbol was forgotten.

10
00:00:36,760 --> 00:00:42,820
Now you want to verify whether what the user provided to you in your code, when you work with that value,

11
00:00:42,820 --> 00:00:45,630
whether that is an email address or not.

12
00:00:45,730 --> 00:00:52,510
Now a trivial test you can make or you can perform is you can use a string method for that, the includes

13
00:00:52,510 --> 00:00:58,390
method and check if that string includes an @ symbol and we would get false here and hence you would

14
00:00:58,390 --> 00:01:05,590
know that the user input we got is not a valid email address but a valid e-mail address is not just

15
00:01:05,590 --> 00:01:08,990
determined by the existence of an @ symbol,

16
00:01:09,070 --> 00:01:14,500
instead we also want to have a domain ending at the end, dot something.

17
00:01:14,500 --> 00:01:18,210
So of course we can also check whether it includes a dot

18
00:01:18,430 --> 00:01:23,860
but that would also mean that something like tests.t@test would be treated a valid email address

19
00:01:24,070 --> 00:01:29,560
because it includes a dot even though it's in the wrong place and that's where regular expressions can

20
00:01:29,560 --> 00:01:30,330
help you.

21
00:01:30,430 --> 00:01:36,430
Regular expressions don't just exist in Javascript, they exist in most programming languages and they

22
00:01:36,430 --> 00:01:40,850
help you search for patterns in strings.

23
00:01:40,850 --> 00:01:49,210
Now you create a regular expression in Javascript in one of two ways, you can create it with the new regex

24
00:01:49,300 --> 00:01:55,180
constructor and then to this constructor, you pass a string which describes the pattern you want to look

25
00:01:55,180 --> 00:02:02,860
for and there is a rich regular expression language and syntax you can use here or you use a literal

26
00:02:02,860 --> 00:02:08,260
notation for creating that regex object, just like you can use square brackets to create an array

27
00:02:08,260 --> 00:02:16,240
object, you use two forward slashes like this and then in between these slashes, you define your pattern and a

28
00:02:16,240 --> 00:02:24,370
simple email validation pattern could look something like this, the carry symbol here, then

29
00:02:24,520 --> 00:02:29,310
\S+@\S, capital S,

30
00:02:29,340 --> 00:02:37,760
that's important in both places, \.\S+$. Now this looks super weird

31
00:02:37,850 --> 00:02:42,430
right now but let's just accept it as it is and make sure you don't mistype.

32
00:02:42,470 --> 00:02:48,410
Now with that, you can use this and as I said, it is an object and call a method on this object

33
00:02:48,410 --> 00:02:54,110
and there we could call the test method and to the test method, you can pass a string where you want

34
00:02:54,110 --> 00:03:02,180
to validate if it meets this pattern and there we could pass in user input, we would get false. On the

35
00:03:02,180 --> 00:03:09,590
other hand, if I test a valid email address, let's say here I test test@test.com, I get true.

36
00:03:09,770 --> 00:03:15,440
Now the reason for that is that this in the end describes a regular expression which says we accept

37
00:03:15,620 --> 00:03:20,280
any kind of word at the beginning,

38
00:03:20,480 --> 00:03:22,780
this symbol means we start from the left

39
00:03:22,790 --> 00:03:27,560
so we have to start with any kind of word, like test,

40
00:03:27,710 --> 00:03:35,470
then somewhere in the overall string I want to have an @ symbol, thereafter again, any kind of word,

41
00:03:35,480 --> 00:03:43,580
so this test here for example, then a dot which we have here and then again, any kind of word with which

42
00:03:43,580 --> 00:03:46,460
we end this string.

43
00:03:46,580 --> 00:03:53,390
This is what this expression means in the end and this string here for example fits into this pattern.

44
00:03:53,420 --> 00:03:58,820
Now of course, there is a rich regular expression syntax you can learn and it's almost like a programming

45
00:03:58,820 --> 00:04:00,650
language on itself.

46
00:04:00,650 --> 00:04:07,670
In the next lecture, I'll provide a brief introduction into this syntax but by no means a complete overview

47
00:04:07,670 --> 00:04:09,640
of all the things you can do there,

48
00:04:09,680 --> 00:04:16,520
however linked to the next lecture, you'll also find an external resource, a little bit of a more detailed

49
00:04:16,520 --> 00:04:22,010
introduction I prepared in the past which you can have a look at and also some MDN documentation

50
00:04:22,010 --> 00:04:28,490
to learn more and I will also show you how you can find out which pattern is best for your use case.
