Hey, and welcome to the regex lesson, and in this lesson, we're going to try to cover off at least
a little bit of regex, there's a whole lot that can be done with regex and there's a lot of different
situations to use regex.
So within JavaScript, you can check to see if the characters match a certain pattern and utilize that
within your code.
And regex is a great way to create patterns that you can look for within strings.
So you've got an example here of regular expressions on regex, of course, is short for regular expressions.
So it creates a regular expression object for matching text with the pattern.
So essentially what you do is you create a pattern and then you look for within a string, for a match,
within the pattern.
So regex is creating a pattern and then checking to see if it matches.
There's a whole bunch of different combinations that you can do in order to fine-tune your pattern that
you're looking for a bunch of different flags.
So global match, ignoring case, multiline dot, all Unicode sticky.
So there's a lot you can do.
And as I said, there is really we're just going to scratch the surface in this lesson because regex
can get fairly complex writing out these patterns and looking for different matches within the patterns.
There is also a really nice website that can help you generate the patterns.
And as you can see, we've got the expression example here, and it's looking to fight and match words
that are matching the pattern.
So it's looking for any uppercase letters and it's taking the entire word that matches and returning
back the words that have started with uppercase.
You can change this as well to A, to Z lowercase, and you're going to see that it's returning back
all that A to Z words with the lowercase.
So exception, there's no uppercase letters being done.
There's a lot of different types of patterns that you can save here.
And the cheat sheet is also super useful because it gives you some really great information on how to
create the patterns.
So I don't spend too much time on the patterns because it does get fairly lengthy.
But I'll show you once you do design the pattern that you're looking for, how you can utilize it with
a JavaScript and pull out information from your strings.
So first, we need to have a string to work with.
So I'm going to just create a string with some random words and characters in here.
And this is going to be similar to the one that we used when we're using the string methods and I'm
going to have JavaScript twice.
So let's create our first expression.
So using the regular expression website in order to generate our expression.
And then we're looking.
The pattern that we're looking for is any of the spaces between the words.
And then looking for another word that's going to be right after that.
So within the tools, we've got an explanation of what's going on.
So that's why this is such a great resource.
That's regex, our dotcom where we're capturing matches.
Any word?
And then next, the word needs to have a whitespace character.
So Space's Tab's line breaks.
And then also we're matching any word character.
So alphanumeric and underscore is all part of a word.
So that's why we get these sets of the two words together as the matches.
But this one here isn't matching because it's got a dot.
This one's got an ampersand.
This one's got a dot as well.
So these ones aren't matching the pattern that we're looking for.
So this is the particular pattern that we're looking for and this is the regular expression that we're
going to look for.
So using the string method of replace, we've got our pattern that we're looking for.
So whereas before you might have been used to using replace if you want to replace, hello, and then
we specify how we want to replace it with what text we want to replace it, and then we can output temp
one into the console.
So you can see now I've replaced the word, but when we bring in regular expressions, we get a little
bit more power to it.
So instead of just doing a regular replace, if we specify the pattern that we want to match.
So it's that regular expression pattern.
Remember, we're matching the two words, hello world.
If we want to replace both those, we could say by people, and that would replace the Hello world.
As it matches the pattern that we're indicating, so essentially it's taking the first two words that
match the pattern and it's updating them with whatever value we're adding into the string.
Let's try a few more so we can console logout.
And taking that same string, we're going to look for a match in the string.
And what we're going to look for is a regular expression.
So we're going to look for any matches that match.
So this is an uppercase G and we've got some information on this particular G.
So we've got the letter that we're looking for, says J.
And matching the pattern.
We've got an index value of 12 sets where it's first located and then we've got the entire input.
So that's the string that we're working with.
If we change this and add some flags to it, so we add GI to it as flags, you can see what gets returned
back is we're returning back all the instance of it, wherever there's G and then as well, this isn't
going to be case sensitive either.
So notice that we're returning back all of the JS.
So all the instances of G get returned back.
And this is a more usable array format where we can loop through all these instances and we also have
a length there.
So that means that we can use a loop in order to iterate through all the and there's a number of other
things that we can do as well.
So within console log so we can pass in our regular expression.
In this case, I'm going to use JavaScript again and I'm going to use test.
So it executes a search for the match between the regular expression and the specified string.
So the regular expression is JavaScript.
So we're testing to see if JavaScript is available within the string and we get back.
True.
So this is returning back a boolean value and we can also check to see if there's any digits.
So any digits.
029 using the regular expression and we can see that there are digits in here if I remove those and
I still get true because I still have a few digits there and once I remove out all of the digits, I
get false.
Let me add them back.
Here's another one that we can try, so there's execute as well and we put our regular expressions.
So even if you place it within a string format like this or a variable, if you if you place it within
a variable or if you write it out directly, same thing.
And then passing in the string that we're using, we see that we're looking for all of the digits.
So we're turning back all of the digits there.
So you get an index of 23.
So if we move these digits, if we have digits here earlier, that's what's going to get returned back
to looking for the first instance of that.
Let's try an array.
So we have a simple array and we'll take doddery and we'll turn into a string and spacing it out with
the dots.
You can do that as well.
So just call this temp for and let's join it and clear the console and temp three.
So now you see that we've got a string with the dots in the middle and this way you can use the regular
expressions with the strings.
If I want to find all the instances of two, I can search and I can return that.
So it executes a pattern on a regular expression.
So whatever word you want to include and then returning that back.
And as you can see, the search is returning back the value of two, and it's seeing that within index
value of seven.
And the difference between search and match is that if we do it within the same thing here, if we do,
we see we get the match being returned back.
So it does have that first match.
And then again, just as we did up here, you can specify to return back all the instances of it and
to not be case sensitive.
So that returns it back with an array format.
And then, of course, we can utilize that within our code.
So I know there's been quite a bit that we've gone through.
So I do encourage you to if you want to read up more about it, you can always check out the Mozilla
Developer Network or regex or dot com, which is an excellent resource, and we'll give you more information
about your regular expressions as you create them.
And you could also I've included a little bit of a cheat sheet here as well.
That gives you some of the more common values that are used, as well as the flags that you can use
in order to create your regular expressions.
So, of course, we do have a challenge for this lesson, and that's to get all of the email addresses
out of this string.
So we've got two email addresses.
You can see them visually.
How do you get them out with code and put them into the console?
So that's the challenge.
You can take a look at this code and we'll walk you through the solution coming up.
And this is the pattern, and I know it's a really long pattern.
So what is essentially doing is looking for all the characters, A to Z, uppercase, lowercase, eight
to zero zero to nine.
So any digits as well as dots underscores and dashes looking for them all the way up to eight and then
again matching that pattern, looking for a dot, separated with a dot and then again matching that
pattern.
So this is for whatever the email address is, some text, that symbol, some more text, another word
dot, and then another word, a set of characters.
So that's what I'm looking for.
Finds all of the matches using match within an array.
And then we loop through the array and output the values into the console, the positive.
We'll try this one out.
It's going into the regex website.
You can see we create the pattern to match looking for all the emails and it highlights them in blue,
checking to see.
And there's also a better explanation here of what we're doing and what we're searching for.
So all the different values that we're looking for and you can see that the emails are coming out.
So if we take the string and if we duplicate the string, you can see it's still grabbing all of the
emails and that's exactly what we want to do.
So let's open up our console and we're going to copy and paste this in.
So update on the string value that we're going to use the string with the emails and let's create our
regular expressions.
So that will be a regular expression, too.
So going back to the website and copy that expression and let's add that in and looking for, gee,
that's a regular expression for the emails that we get our email data within a variable.
So taking the my string to string and then looking for a match and matching regular expression to that,
we can now put it into the console.
So see, we've got all the emails from the string coming in.
Now we can use just a regular Falu, and while X is less than the length of email data and increment
X by what, we can output all of the emails into the console, this format.
So we've got all of them within the email data.
And as we're iterating looping through, you can see output the emails.
So even if we had even more emails here, you can see they're also going to get output into the console.
So now we've got three emails.
They also get output.
So awfully dynamic.
So hope you had an opportunity to try this out and get more familiar with working with regex.