Coding

Regular Expressions Introduction | REGEX DEMYSTIFIED

  • 00:00:01 welcome to this video great to have you
  • 00:00:04 on board in this in the next videos
  • 00:00:06 I want to demystify regular expressions
  • 00:00:09 regular expressions can be very useful
  • 00:00:11 but also can be intimidating we all know
  • 00:00:14 these super long regular expressions we
  • 00:00:17 use to validate an email address now
  • 00:00:19 let's have a look at what regular
  • 00:00:22 expressions actually are and why we want
  • 00:00:23 to use them before we then dive into how
  • 00:00:26 we construct more and more difficult and
  • 00:00:28 complex patterns and how we can think
  • 00:00:31 about regular expressions to really
  • 00:00:33 understand them let's start
  • 00:00:38 let's start with the what regular
  • 00:00:40 expressions are I'm on code pannier
  • 00:00:43 created a new pan and now let's say we
  • 00:00:45 have a text like hello there and this is
  • 00:00:50 a normal string you could imagine that
  • 00:00:52 it was entered by a user into an input
  • 00:00:55 field and now we have it stored in a
  • 00:00:57 variable or constant here now let's say
  • 00:01:00 we want to see if that string we got by
  • 00:01:02 the user contains two else and obviously
  • 00:01:06 there are different ways of achieving
  • 00:01:08 doesn't JavaScript we can of course use
  • 00:01:11 non regular expression ways but one of
  • 00:01:13 the easiest ways is to construct a
  • 00:01:15 regular expression and well check this
  • 00:01:18 this also answers the question what a
  • 00:01:21 regular expression is it's just a
  • 00:01:23 pattern a pattern of characters in the
  • 00:01:26 end you use it to check strings for that
  • 00:01:31 pattern
  • 00:01:31 if the pattern finds a hit in a given
  • 00:01:34 string a regular expression can be
  • 00:01:36 constructed in two different ways in
  • 00:01:38 JavaScript I'll store it in a new
  • 00:01:40 constant the first way is to create a
  • 00:01:42 new rec X object this reg FX rec X
  • 00:01:46 constructor is a default object provided
  • 00:01:50 in JavaScript it takes an argument and
  • 00:01:53 that's a string and there you will
  • 00:01:56 define the pattern you want to check for
  • 00:01:58 for example ll and thereafter we can log
  • 00:02:02 something to the console we can take our
  • 00:02:04 regular expression stored in that regex
  • 00:02:07 constant and execute test and now pass
  • 00:02:11 text as an argument and there you see
  • 00:02:14 finally at the end I get a true code pan
  • 00:02:18 automatically updates this so I get a
  • 00:02:20 true because I find two else in hello
  • 00:02:23 there if I now clear the console and
  • 00:02:26 remove one L in hello there you will see
  • 00:02:29 I get a false because now we got no hit
  • 00:02:31 we don't find this this in the end is
  • 00:02:34 what a regular expression is a pattern
  • 00:02:36 we can check we can check on a string
  • 00:02:39 now I mentioned that there are two ways
  • 00:02:43 of creating a regular expression you can
  • 00:02:45 use that constructor style here or let
  • 00:02:49 me store this in the same constant after
  • 00:02:51 comment
  • 00:02:52 I ever went out you can create a regular
  • 00:02:54 expression in JavaScript with two
  • 00:02:56 forward slashes and even though it looks
  • 00:02:59 strange
  • 00:02:59 this constructs a new object a new
  • 00:03:02 regular expression object it's just some
  • 00:03:05 syntactical sugar javascript offers it
  • 00:03:08 in between the forward slashes you
  • 00:03:10 define the pattern without quotation
  • 00:03:12 marks so you don't pass a string here
  • 00:03:14 you just type the pattern like ll that's
  • 00:03:18 our pattern and therefore the code still
  • 00:03:20 works and if I riad ii l in hello there
  • 00:03:25 we get a true again so these are the two
  • 00:03:28 different ways of creating a regular
  • 00:03:30 expression javascript and that's what we
  • 00:03:33 do with a regular expression now there
  • 00:03:35 are more methods than just tests we also
  • 00:03:37 have x act for execute and this will
  • 00:03:40 return an array with the hit it found
  • 00:03:43 like the two else our pattern basically
  • 00:03:45 and we can also reword this and take our
  • 00:03:50 text and there for example called match
  • 00:03:52 and pass our regular expression so now
  • 00:03:56 this will give us any hit it has
  • 00:03:58 basically the reversed version of x AK
  • 00:04:01 or we have search and search will give
  • 00:04:05 us the index where we find this
  • 00:04:08 expression so ll starts at index two in
  • 00:04:11 the text let's have a look and indeed
  • 00:04:14 their index 1 would be th as 0 excuse me
  • 00:04:19 and x 0 would be th index 1 would be de
  • 00:04:21 and then next 2 st starting point of
  • 00:04:23 that pattern dl now of course regular
  • 00:04:28 expressions are even more useful if you
  • 00:04:30 want to find more complex patterns if
  • 00:04:33 you for example wanna find any word that
  • 00:04:35 contains an @ sign or stuff like that
  • 00:04:38 these are things were going to dive into
  • 00:04:41 the next videos off this series have a
  • 00:04:43 look at how we build more and more
  • 00:04:45 complex patterns but i hope with this
  • 00:04:47 video and this short example it's
  • 00:04:49 already a bit clearer why we use regular
  • 00:04:52 expressions and how we use them we want
  • 00:04:55 to find out if a given text matches a
  • 00:04:58 certain condition in the end and the
  • 00:05:00 condition is simply a pattern now with
  • 00:05:02 that let's dive into how these patterns
  • 00:05:05 constructed starting in the next video