1 00:00:00,320 --> 00:00:05,210 Flags in regular expressions modify the way the expression behaves. 2 00:00:05,210 --> 00:00:09,070 There are a number of flags, but we'll focus on three. 3 00:00:09,070 --> 00:00:14,280 Each one of these flags overrides a default behavior of regular expressions. 4 00:00:14,280 --> 00:00:18,050 First, i stands for case-insensitive. 5 00:00:18,050 --> 00:00:23,010 Using i will make the parser disregard case when searching for matches. 6 00:00:23,010 --> 00:00:24,720 G is for global. 7 00:00:24,720 --> 00:00:29,500 By default, the regular expression will stop searching once it finds a match. 8 00:00:29,500 --> 00:00:34,070 The g flag tells the parser to find all matches contained in a string. 9 00:00:34,070 --> 00:00:36,948 The m flag stands for multiline. 10 00:00:36,948 --> 00:00:41,195 Normally a caret will only match the very beginning of a string and 11 00:00:41,195 --> 00:00:44,010 a dollar sign will only match the end. 12 00:00:44,010 --> 00:00:48,780 Any line breaks the string contains will be considered part of the string. 13 00:00:48,780 --> 00:00:53,850 Setting the m flag will cause the parser to treat line breaks like new lines. 14 00:00:53,850 --> 00:00:57,160 A caret will match the beginning of each new line and 15 00:00:57,160 --> 00:00:59,000 a dollar sign will match the end. 16 00:00:59,000 --> 00:01:03,770 To add any of these flags to a regex in JavaScript, 17 00:01:03,770 --> 00:01:07,250 put them after the last slash of a regex literal. 18 00:01:07,250 --> 00:01:08,640 You can put them in any order. 19 00:01:09,790 --> 00:01:11,330 Let's see these in use. 20 00:01:11,330 --> 00:01:16,940 I'm still in my console, I'll create a new string LION 21 00:01:16,940 --> 00:01:19,820 in all caps and call replace on it. 22 00:01:20,850 --> 00:01:26,530 I'll pass a regex matching lion in lowercase and add the i flag to it. 23 00:01:28,440 --> 00:01:31,503 And let's make the replacement string mouse. 24 00:01:33,341 --> 00:01:35,914 You can see LION was replaced. 25 00:01:35,914 --> 00:01:40,002 Note, you could also use a character set containing a to z. 26 00:01:44,190 --> 00:01:46,665 All lowercase. 27 00:01:46,665 --> 00:01:48,750 Let's use the global flag. 28 00:01:48,750 --> 00:01:50,430 I'll use a slightly different string. 29 00:01:50,430 --> 00:01:55,138 I'll clear my console and type, 30 00:01:55,138 --> 00:02:00,526 she ate watermelon at the waterpark. 31 00:02:03,304 --> 00:02:06,407 I'll look for water and replace it with an empty string. 32 00:02:12,008 --> 00:02:15,500 In other words, I want to delete the string, water. 33 00:02:15,500 --> 00:02:19,600 Notice how the parser found and replaced the first occurrence of water and 34 00:02:19,600 --> 00:02:21,670 left the second one alone. 35 00:02:21,670 --> 00:02:26,410 If we wanted the parser to replace all occurrences, we could add a g flag. 36 00:02:28,510 --> 00:02:31,620 Now both occurrences of the name have been replaced. 37 00:02:31,620 --> 00:02:34,520 If there were more, they would have been replaced too. 38 00:02:34,520 --> 00:02:38,961 Now I'll create a variable called treat and I'll store a multiline string in it. 39 00:02:41,805 --> 00:02:44,583 Each line of the string is the word cheese. 40 00:02:49,242 --> 00:02:54,992 Now I'll call replace on it and pass in the regular expression 41 00:02:54,992 --> 00:03:00,088 matching a string that only contains the word cheese. 42 00:03:04,115 --> 00:03:05,520 I'll replace it with fruit. 43 00:03:08,382 --> 00:03:11,910 The regex doesn't match it because the string contains the word 44 00:03:11,910 --> 00:03:15,040 cheese three times with new lines. 45 00:03:15,040 --> 00:03:19,280 If I add the m flag, 46 00:03:19,280 --> 00:03:23,570 you can see that the first instance of cheese was changed to fruit. 47 00:03:23,570 --> 00:03:25,400 The parser found the line break and 48 00:03:25,400 --> 00:03:28,710 considered the first instance of cheese to match. 49 00:03:28,710 --> 00:03:32,755 The other instances were not matched because we didn't set the global flag. 50 00:03:33,930 --> 00:03:35,651 I'll put a g now. 51 00:03:37,489 --> 00:03:40,530 All of the instances of cheese are turned to fruit. 52 00:03:40,530 --> 00:03:44,000 You'll see flags often in other regular expressions and 53 00:03:44,000 --> 00:03:45,710 it's good to know about them. 54 00:03:45,710 --> 00:03:50,850 Later in the course for example, we'll make use of the case-insensitive flag. 55 00:03:50,850 --> 00:03:55,658 I'd probably use the global flag most of all to replace occurrences of a name, for 56 00:03:55,658 --> 00:03:58,120 example, or to delete blank lines. 57 00:03:59,750 --> 00:04:04,790 You're ready to start adding validation to the form I showed in an earlier video. 58 00:04:04,790 --> 00:04:08,450 In the next video, I'll show you the code we'll be starting with. 59 00:04:08,450 --> 00:04:11,530 Then, we'll validate the user name input. 60 00:04:11,530 --> 00:04:12,350 Let's get started.