WEBVTT 00:00:07.180 --> 00:00:11.420 In this video are we going to learn regular expressions or rejects. 00:00:11.610 --> 00:00:15.140 So what do we need rejects for. 00:00:15.210 --> 00:00:19.950 They aren't used usually to look for a certain pattern in a string. 00:00:20.520 --> 00:00:24.390 Basically they can be used to validate a certain user inputs. 00:00:24.450 --> 00:00:30.840 So let's say would like the user input to include at least one capital letter then we'd need to use 00:00:31.070 --> 00:00:32.820 projects for that. 00:00:32.830 --> 00:00:39.360 Also if you would like to make sure that the user inputs includes at least one digits we would need 00:00:39.360 --> 00:00:41.320 to use a rejects. 00:00:41.340 --> 00:00:42.490 All right. 00:00:42.720 --> 00:00:44.970 So let's start with one first example. 00:00:44.970 --> 00:00:46.960 First of all we're going to need a string. 00:00:47.480 --> 00:00:51.180 So let's define a string so to call it's a C or. 00:00:51.660 --> 00:00:53.590 Give it a value. 00:00:53.600 --> 00:00:55.600 Let's go for Atlanta for instance. 00:00:55.950 --> 00:00:56.490 OK. 00:00:56.580 --> 00:00:59.020 And then we're going to need a pattern. 00:00:59.130 --> 00:01:01.200 So the pattern is an object. 00:01:01.470 --> 00:01:12.990 OK so it's not a string it's an object so let's call it pattern and let's go for example four letter 00:01:13.170 --> 00:01:13.780 A. 00:01:14.100 --> 00:01:16.790 So the Post needs to be between two four slashes. 00:01:16.920 --> 00:01:17.980 OK. 00:01:18.960 --> 00:01:22.170 And we can add a modifier at the end of the button. 00:01:22.470 --> 00:01:28.940 Ok so the modifier will modify the way we're going to search for the person inside the string. 00:01:28.950 --> 00:01:34.420 So for example it is used to do a case insensitive search. 00:01:34.650 --> 00:01:40.200 OK so if we're looking for a then it will take into account any a we find there whether it's a copy 00:01:40.200 --> 00:01:42.140 of a letter or a small letter. 00:01:42.630 --> 00:01:43.140 OK. 00:01:43.500 --> 00:01:48.180 So now let's see how to search for the pattern inside the string. 00:01:48.420 --> 00:01:52.560 So we're going to send an alert message with the results. 00:01:53.010 --> 00:02:00.030 OK so one way of doing that is by using a string method so we can start with the string. 00:02:00.780 --> 00:02:01.680 OK. 00:02:01.870 --> 00:02:10.740 And we're going to use a method called search and then we're going to use the button in the promise 00:02:10.740 --> 00:02:11.760 of the surge. 00:02:11.760 --> 00:02:15.180 So basically researching the pattern inside the string. 00:02:15.300 --> 00:02:20.610 So let's see what the results is going to be. 00:02:20.610 --> 00:02:21.990 So we get zero. 00:02:22.290 --> 00:02:29.700 Basically the search method returns the position of the first instance of the bus and thus we could 00:02:29.700 --> 00:02:30.520 find in the string. 00:02:30.540 --> 00:02:34.840 And in our case we found a letter a in position 0. 00:02:34.920 --> 00:02:36.930 But why is returning 0. 00:02:37.600 --> 00:02:40.610 And it's a case insensitive search. 00:02:40.620 --> 00:02:43.450 So basically it's taken into account the capital letter as well. 00:02:43.770 --> 00:02:52.470 OK so if we change the search to a global search using the letter then this time the results are going 00:02:52.470 --> 00:02:59.040 to be 3 because a the first eight doesn't count because it is so they're saying we're looking for small 00:02:59.040 --> 00:02:59.880 letters. 00:02:59.880 --> 00:03:03.930 So the first instance in our case now has got a position three. 00:03:04.190 --> 00:03:05.440 OK. 00:03:05.600 --> 00:03:13.350 All right so let's keep the modifier G also when we use the G as a modifier when we are doing a much 00:03:13.980 --> 00:03:14.790 later on. 00:03:14.880 --> 00:03:18.740 There is a small difference in the results and we'll see it in the biz. 00:03:19.170 --> 00:03:28.110 So now there is another function is the much so it's also a method of string and the Paromita is the 00:03:28.110 --> 00:03:28.680 parson. 00:03:28.860 --> 00:03:31.220 So let's see what the result is. 00:03:32.100 --> 00:03:38.500 So first let's only Let's try the I modify it and see what we get. 00:03:38.850 --> 00:03:48.090 OK so it's returning the first instance of the search so returning the actual letter that we could find 00:03:48.420 --> 00:03:49.550 according to the person. 00:03:49.710 --> 00:03:50.310 OK. 00:03:50.310 --> 00:03:56.550 Now if we change the modifier to G it's a global search. 00:03:56.550 --> 00:04:05.690 This time it's going to return all the instances of the search which is at this time case sensitive 00:04:05.690 --> 00:04:05.900 . 00:04:05.940 --> 00:04:07.440 So it's this one. 00:04:07.640 --> 00:04:08.200 And that's one. 00:04:08.310 --> 00:04:10.820 So we've got a list of characters there. 00:04:11.190 --> 00:04:12.200 OK. 00:04:12.870 --> 00:04:13.370 OK. 00:04:13.860 --> 00:04:18.720 Now there is another way of testing a person in a string. 00:04:18.720 --> 00:04:25.540 It's by using the test method which is a method of the person itself. 00:04:25.560 --> 00:04:26.400 A lot of the string. 00:04:26.400 --> 00:04:29.850 So basically we need to change the whole syntax. 00:04:29.850 --> 00:04:33.460 We're going to start with the actual with the possum itself. 00:04:33.720 --> 00:04:39.420 Using a method or the passing of the function or the method is test then the Brahman's is going to be 00:04:39.510 --> 00:04:40.350 the string. 00:04:40.630 --> 00:04:43.470 Ok so let's see what the results are going to be. 00:04:43.830 --> 00:04:46.080 And this function is returning a boolean. 00:04:46.330 --> 00:04:53.400 So basically it will just test the pattern in the string and see whether it exists or not and it will 00:04:53.520 --> 00:04:56.250 return true or false. 00:04:56.250 --> 00:04:57.090 OK. 00:04:57.330 --> 00:05:03.270 All right so now we going to create a few paragraphs. 00:05:03.270 --> 00:05:04.140 OK. 00:05:04.290 --> 00:05:10.710 I going to create some some coding here and just play with the parsons and strings and just make sure 00:05:10.710 --> 00:05:15.320 that we've got the results on the page to make it easy understand. 00:05:15.330 --> 00:05:18.780 So I'm just going to get rid of everything here. 00:05:19.140 --> 00:05:23.020 OK we're going to keep the string and pass in early. 00:05:23.280 --> 00:05:31.310 OK so let's create a paragraph first paragraph is going to be the string. 00:05:32.490 --> 00:05:33.710 OK. 00:05:34.650 --> 00:05:38.010 And then another paragraph for the pattern. 00:05:38.250 --> 00:05:41.880 Another one for the results using search results using much. 00:05:41.880 --> 00:05:45.140 Another one for the results using the test method. 00:05:45.150 --> 00:05:48.340 So first one is going to be string. 00:05:48.920 --> 00:05:50.410 OK. 00:05:50.970 --> 00:06:00.750 And we're going to put the string in a span on right when we need to give the band an ID with an ID 00:06:00.810 --> 00:06:01.260 of 00:06:04.790 --> 00:06:07.460 a string. 00:06:09.080 --> 00:06:10.100 OK. 00:06:10.920 --> 00:06:14.410 The next one is going to be the bottom. 00:06:14.820 --> 00:06:18.990 And also he put the button in the span as well 00:06:21.870 --> 00:06:24.220 and he gives you the idea as well. 00:06:25.800 --> 00:06:32.940 All right the next one is going to be for the search result. 00:06:35.250 --> 00:06:40.350 So we put it in a span with an ID 00:06:43.380 --> 00:06:50.130 search only easier to just duplicate those. 00:06:50.130 --> 00:06:56.950 So the other one is search much is much. 00:06:56.980 --> 00:07:02.020 And the final one is test ID test. 00:07:02.430 --> 00:07:03.610 OK. 00:07:04.680 --> 00:07:05.110 Now 00:07:08.010 --> 00:07:09.360 OK. 00:07:09.500 --> 00:07:14.170 Now we're going to change the contents of the string to see our. 00:07:14.640 --> 00:07:27.340 OK so documents gets elements point Id string and we want to change d in a shemale to see. 00:07:27.710 --> 00:07:28.740 OK. 00:07:28.900 --> 00:07:32.850 And the same thing for the puzzle. 00:07:33.970 --> 00:07:37.690 OK. 00:07:38.040 --> 00:07:40.680 The same thing. 00:07:42.090 --> 00:07:46.200 While the search much does. 00:07:51.870 --> 00:07:59.470 So basically for the search we're going to use the search method so it's going to be the string. 00:08:00.540 --> 00:08:07.670 I mean using the search method and the barometer is the person OK 00:08:11.730 --> 00:08:13.160 for the match. 00:08:13.770 --> 00:08:17.850 It's going to be similar as the method is much. 00:08:17.910 --> 00:08:26.610 And finally finally the results using the test is by starting with the puzzle and using the test method 00:08:26.700 --> 00:08:30.430 which is a method of the puzzle itself. 00:08:30.890 --> 00:08:32.810 So it's what we get here. 00:08:33.510 --> 00:08:38.670 All right so now we can see the string we can see the bottom and we can see the results using each of 00:08:38.670 --> 00:08:40.730 the methods. 00:08:40.740 --> 00:08:41.950 OK. 00:08:42.000 --> 00:08:47.100 So let's change let's change the button before we do that. 00:08:47.120 --> 00:08:51.980 There is another way of defining the person using a constructor is the regex constructor. 00:08:52.170 --> 00:08:55.940 So the syntax is var. 00:08:56.500 --> 00:09:06.030 OK the name of the parson and we use new followed by the average expansion or constructor and we need 00:09:06.450 --> 00:09:07.840 two performances there. 00:09:07.860 --> 00:09:14.010 So the first parameter is going to be whatever we put between the two for slushes that is going to be 00:09:14.010 --> 00:09:14.900 a string. 00:09:15.120 --> 00:09:16.440 Ok so a. 00:09:16.620 --> 00:09:19.250 And the second part I would say is the modifier. 00:09:19.380 --> 00:09:23.050 And we need to use strings OK for both parameters. 00:09:23.070 --> 00:09:28.520 So basically if we Colman's that's one I see the result is going to be exactly the same. 00:09:29.100 --> 00:09:34.890 But for now we're just going to use the simple way that we have seen at the beginning. 00:09:34.890 --> 00:09:39.170 OK so let's change the parts in and see what results we're going to be getting. 00:09:39.360 --> 00:09:43.940 So now rather than having a person of letter A so we can change this. 00:09:43.950 --> 00:09:51.570 So let's say we want to check whether the string has called any letter between a and said any letter 00:09:51.570 --> 00:09:51.970 . 00:09:51.990 --> 00:09:58.810 So the syntax is going to be a minus sign Zed between brackets. 00:09:58.980 --> 00:10:00.910 OK so if we take that 00:10:03.880 --> 00:10:12.560 so basically the results shows that's the first instance it's position 1 which is t and it's because 00:10:12.620 --> 00:10:15.340 we're looking at only small letters. 00:10:15.940 --> 00:10:17.390 And the results much. 00:10:17.410 --> 00:10:20.370 All the letters of the capitalist A. 00:10:20.440 --> 00:10:20.930 They are. 00:10:20.970 --> 00:10:26.370 But is OK and we could see them that list the test results is true. 00:10:26.790 --> 00:10:27.340 OK. 00:10:27.700 --> 00:10:30.600 OK let's try another puzzle. 00:10:31.900 --> 00:10:32.780 OK Troy. 00:10:32.860 --> 00:10:33.900 80. 00:10:34.360 --> 00:10:34.880 OK. 00:10:35.110 --> 00:10:39.650 And then we're checking whether the string includes these letters. 00:10:39.700 --> 00:10:40.290 OK. 00:10:40.310 --> 00:10:47.570 And now if we use the carrot's symbol that means that we're looking for all the characters that are 00:10:47.590 --> 00:10:48.540 not in here. 00:10:48.670 --> 00:10:49.150 OK. 00:10:49.450 --> 00:10:51.280 So let's see what the result is going to be. 00:10:51.740 --> 00:10:58.150 OK so we can see that we were looking so the first instance is 0 because that's character is not there 00:10:58.410 --> 00:11:01.550 I was looking for anything outside these letters. 00:11:01.770 --> 00:11:02.020 OK. 00:11:02.020 --> 00:11:06.880 All these characters so much is capital A and L. 00:11:07.190 --> 00:11:08.680 OK. 00:11:08.750 --> 00:11:20.260 OK now let's try something else just common that this try to look for digits between four and seven 00:11:20.270 --> 00:11:20.930 . 00:11:21.580 --> 00:11:22.260 OK. 00:11:22.450 --> 00:11:25.080 So it is change this a little bit and something is there. 00:11:25.420 --> 00:11:32.410 So we're looking for all the difference between four and seven which means four five six and seven. 00:11:32.400 --> 00:11:34.730 OK so let's see. 00:11:35.650 --> 00:11:41.570 OK so the result the first instance of position 7 and we can see the results there. 00:11:41.620 --> 00:11:45.900 You know the list of all the instances and the tests is returning true. 00:11:46.270 --> 00:11:47.130 All right. 00:11:47.670 --> 00:11:51.790 OK so let's try something else. 00:11:51.820 --> 00:11:57.980 Let's try now to see if there is any digits inside our string so the syntax for that is. 00:11:58.000 --> 00:11:59.440 So we know we don't need brackets. 00:11:59.530 --> 00:12:03.640 We only need a backslash with letter D. 00:12:03.690 --> 00:12:06.970 Let's see what we get the results. 00:12:07.480 --> 00:12:09.600 OK so it's finding digits there. 00:12:09.810 --> 00:12:10.300 OK. 00:12:10.480 --> 00:12:12.950 So it says it's true and we can see all the answers there. 00:12:12.940 --> 00:12:16.320 Now we can try something else. 00:12:16.330 --> 00:12:19.950 We can check whether this string has got any letter characters. 00:12:19.960 --> 00:12:26.050 OK so this is done using the backslash and then small w. 00:12:26.440 --> 00:12:27.720 OK. 00:12:29.230 --> 00:12:31.110 You can see those from the first character. 00:12:31.270 --> 00:12:33.800 We are verifying the condition and we can see those. 00:12:33.820 --> 00:12:36.110 All of them are characters. 00:12:36.400 --> 00:12:43.070 If you check something difference or is it a special character. 00:12:43.620 --> 00:12:47.810 So now we can see that the special character is most in our matches. 00:12:47.950 --> 00:12:48.480 OK. 00:12:48.970 --> 00:12:49.360 OK. 00:12:49.370 --> 00:12:56.980 Also if we change the small w to a capital W then that's different. 00:12:56.970 --> 00:13:01.800 So now we're looking for all the characters other than ways other than letters. 00:13:01.880 --> 00:13:08.900 OK so let's check that's the only much now is the percentage. 00:13:09.190 --> 00:13:11.230 OK. 00:13:11.260 --> 00:13:15.590 All right so let's try something else. 00:13:15.580 --> 00:13:17.460 No it's going to be OK. 00:13:17.800 --> 00:13:20.530 So let's look for a space. 00:13:20.530 --> 00:13:28.030 So the same sex is backslash s and if we try that no the result is false because we don't have any space 00:13:28.180 --> 00:13:29.150 in our string. 00:13:29.230 --> 00:13:34.640 But if we are in a space there are two spaces then we can see that. 00:13:35.000 --> 00:13:39.140 Now we can see that the first space is position 7. 00:13:39.250 --> 00:13:41.460 And the result is true. 00:13:41.530 --> 00:13:42.450 All right. 00:13:42.820 --> 00:13:46.200 OK. 00:13:49.100 --> 00:13:50.930 Now let's look for 00:13:55.220 --> 00:13:56.030 digits. 00:13:56.200 --> 00:13:56.470 OK. 00:13:56.470 --> 00:13:59.990 But this time we're looking for four digits. 00:14:00.040 --> 00:14:05.480 So basically the syntax is the original syntax of looking four digits followed by curly brackets and 00:14:05.470 --> 00:14:09.720 inside the curly brackets we put the number of instances that we are looking for. 00:14:09.910 --> 00:14:10.530 OK. 00:14:10.960 --> 00:14:12.580 So let's say let's let's see. 00:14:12.670 --> 00:14:14.110 There there always going to be. 00:14:14.120 --> 00:14:17.640 So it is true because we have four there there. 00:14:17.760 --> 00:14:18.070 OK. 00:14:18.070 --> 00:14:19.920 And this is the results. 00:14:20.440 --> 00:14:21.660 OK. 00:14:21.670 --> 00:14:25.460 Now let's look for 10 digits. 00:14:25.920 --> 00:14:27.210 It doesn't work well. 00:14:27.250 --> 00:14:33.420 Basically the test returns false because we don't have 10 digits in our string. 00:14:33.730 --> 00:14:35.100 OK. 00:14:35.260 --> 00:14:39.620 All right so let's try something else. 00:14:39.730 --> 00:14:48.110 We want to test whether the string has caught capital A as the first character. 00:14:48.130 --> 00:14:48.940 OK. 00:14:49.580 --> 00:14:55.790 And we would need to use a character for that so it's a forward slash at the beginning. 00:14:56.020 --> 00:14:57.330 All right. 00:14:58.100 --> 00:14:59.150 OK now it's true. 00:14:59.140 --> 00:15:07.290 So if we check if we change capital A to capital B or any other letter it doesn't work. 00:15:07.340 --> 00:15:09.440 OK. 00:15:09.430 --> 00:15:20.920 And finally we can also check whether we have a line break character inside our string using a backslash 00:15:22.510 --> 00:15:22.750 and 00:15:25.940 --> 00:15:30.090 Stigler's is returning false. 00:15:30.190 --> 00:15:30.920 If we change 00:15:33.970 --> 00:15:40.500 this just have a line break. 00:15:41.360 --> 00:15:41.880 Because 00:15:45.680 --> 00:15:48.020 now he's working right. 00:15:48.350 --> 00:15:51.990 OK so this has shown us how rejects works. 00:15:52.030 --> 00:15:59.980 It's quite powerful to validate user inputs and we'll see some examples later on they will be used mainly 00:15:59.980 --> 00:16:05.140 to validate e-mails or other inputs like telephone numbers and so on