Placeholder Image

字幕列表 影片播放

  • hello and welcome to this course on regular expressions.

  • Regular expressions or rejects define a search pattern that could be used to search for things in a string.

  • Now this course focuses on using regular expressions in JavaScript.

  • However, almost everything you learn about regular expressions in this course can also apply.

  • If you're using regular expressions in another programming language, my name is Bo Kearns.

  • I will be teaching this course I'm with free Coke, camped out or GE.

  • In fact, this whole course is based on the free curriculum at Freak Oh camped out Warg.

  • It's not required to go through that curriculum to also do this course, but it could be helpful.

  • However, this course is designed to be stand alone.

  • But if you want to go through the curriculum on Freak, oh, camped out organs well, you can look in this picture where it has a row right to the regular expressions part of the curriculum, so you can see where that's at.

  • Well, let's get started.

  • Regular expressions are used in programming languages to match parts of strings.

  • You create patterns to help you do that matching So, for instance, if you have the sentence the dog chased the cat and you want to match the word the you can use This rejects here.

  • You can see that there's a slash in the word the in another slash You will also notice that quote marks are not required within the regular expression.

  • So JavaScript has multiple ways to use Reg X is one way is to test the rejects is to use the test method.

  • The test method takes the rejects, applies it to a string and then returns true or false, if the pattern matches something.

  • So in this example, we have the string Hello world.

  • We have this rejects and we have my rejects.

  • I'm gonna do dot test and then inside the parentheses I'm gonna put my string.

  • So we're going to see if my string contains this rejects, which is hello.

  • And then we'll put it into the result.

  • In this case, it will be true because hello world does contain the string.

  • Hello Here.

  • We want to match the word Waldo.

  • We have this sentence somewhere.

  • Waldo is hiding in this text.

  • So I'm gonna change this rejects instead of search.

  • It's going to be Waldo now.

  • One thing I wanna point out is that this is case sensitive.

  • So if I had a lower case W, then this would not match.

  • But since this starts with an uppercase W, I have to make the Rejects start with an uppercase w.

  • Also so there's a result the Waldo Reg IX dot test.

  • While Waldo is hiding, it's going to return.

  • True because it found Waldo in this sentence.

  • Rejects has an or operator, which is a single pipe character.

  • So in this example, let's say we want to try to match for multiple words instead of just a single word.

  • Let's say we want to match dog, cat, bird or fish.

  • So what I would do here is I'm going to change this.

  • I'll do dog and it'll put hype, which is just a straight line up and down.

  • And then I'll do cat I bird like fish.

  • So this is going to match any of these four words dog or cat war, bird or fish?

  • And since one of these words is in the sentence, this test is going to return true.

  • So far, all the reject says we looked at matched literal strings, so the case mattered.

  • If it was a capital letter that's completely different from a lower case letter.

  • You can match both cases using what is called a flag.

  • So there's the I flag that ignores case.

  • So let me show you how you would do that.

  • Let's say we have a free code camp and we'll see it's all lower case in here, but we want a match it no matter what the case.

  • So we want this to also match this where there's a Capital C and another capital C.

  • So just after the last slash, I'm just going to put an eye.

  • So this is the flag.

  • This means ignore case.

  • So now when it tests the string freak Oh, camp for the rejects, which is freak Ocampa, lower case, the result will be yes, because they will match.

  • So far, we have just checked if a pattern exists or not within a string.

  • But we can also extract the actual matches that we found with the match method.

  • So let's see how to extract the word coding from this string.

  • So we'll just change this to coating and then here will use the dot match Method match and then in the parentheses.

  • Will passion coding Reg X and then if we just run this, we'll see in the console coating because it's logging out.

  • The results, either result extracted.

  • This were coating out of the sentence and into the result variable, and it got logged.

  • So far, we've only extracted search patterns one time.

  • But it is also possible to search or extract a pattern more than once using the G flag.

  • Let me show you what I mean.

  • We have this test string up here.

  • Repeat, repeat, Repeat.

  • Now we're going to search for a repeat.

  • And the wait is now.

  • When it matches this, it's on Lee going to return the first Repeat just this one.

  • But if we add the G flag after the slash here, it's going to find every single occurrence of the word.

  • Repeat.

  • So it's going to return an array with three elements and each element is gonna be the word.

  • Repeat.

  • Let's see it again down here.

  • Where says twinkle, twinkle, little star.

  • I want to match the word twinkle so I'll type and twinkle right here and you'll see that the first week was capitalized.

  • The 2nd 1 is not so.

  • I'm gonna have to use a flag out to use the I flag, which makes it insensitive to case.

  • So it's case insensitive.

  • I'm also gonna add the G flag that's gonna match every single time Twinkle appears, so you can use to flags at once on the same rejects.

  • Now here, for the result is going to twinkle star dot match And then I'm going to pass in the star Reggie Dex.

  • Okay, now we'll log this and see what happens.

  • You can see twinkle, twinkle The first twinkle in the console is the Capital T and the second's a lower case because it matched the 1st 1 and the 2nd 1 and returned them both.

  • In Reg IX, a period is a wild card character.

  • It can stand for anything.

  • So let me show you an example.

  • We have these two strings up here, and the 1st 1 has the word hum.

  • The 2nd 1 has the word hug.

  • You'll notice that these two words start with the same two letters h u.

  • And then the third letter is different.

  • So look at this Rejects here h u period.

  • This will match any word that starts with the characters H.

  • U and then then has a final character, which could be any character.

  • So if we look down here, we are going to take the first string Al Hama song and match it to our rejects, its going to return home.

  • And then when we take our second string bear hug and match it to our rejects, its going to return hug.

  • So let's try that with another sentence.

  • We have the sentence here.

  • Let's have some fun with regular expressions, so I want to match the word fund.

  • But I also want this rejects to be able to match the word run son pond, non or bun, Basically any word that ends with you in and starts with another letter, so I'll change this rejects to dot u.

  • N.

  • So when we run our Reg X and search this sentence for something that ends with you in, it should find fun.

  • So let's load, then look in the console and it says, true, because it matched the word fund.

  • In that sentence, we talked about the wild card character, which matches basically anything, but you can also match from a pre defying group of characters.

  • If we look at this right here.

  • It we have the b and then we have these brackets and inside the brackets were saying that the second letter that's gonna match has to be one of these three letters.

  • So we know the first letters.

  • Let me be.

  • The last letter is gonna be G.

  • And the middle letter is gonna be a I or you.

  • So this would match bag big and bug.

  • So down here, what we want to do is change this.

  • So we match every single avowal in this quote sample here.

  • So what I'm gonna do is put open and close brackets and inside I'm gonna put the vowels a e i o u.

  • And then to make sure I match every single vowel and I am match uppercase or lower case fouls, I'm gonna use flags.

  • I'll use the I flag and the G flag.

  • Now, down here, I'm going to have to do quote, sample that match, and then I just pass in the valve rejects.

  • Here, I run this in the console.

  • You'll see all the vowels from that quote sample.

  • You can also match a range of letters.

  • So let me show you how to do that right here.

  • I'm gonna try to match every single letter.

  • So I'm going to put the open and close brackets.

  • And instead of typing a B C D e f g and rang every letter, I could just put a through Z.

  • So it's gonna match every letter between A and Z and then to make sure it matches every uppercase and lowercase letter.

  • I'll do I and then G to make sure it matches every letter in the whole string.

  • So now I could just do quote, sample maps and then I just pass in the alphabet, rejects here.

  • And if I run that we see in the consulate has every letter in that string, just like you can use the rejects to match a range of letters, you can also match a range of numbers.

  • So if I want a match range of numbers, it will be like this.

  • I'll have the brackets here, the opening closing brackets.

  • And this time I'm just gonna match the numbers two through six.

  • So just put to death six.

  • So that will match 23456 also in the same Reg X.

  • I can match a range of numbers and a range of letters.

  • So if I want to add some letters, I can put H through s.

  • So now we can see this is going to match a range of numbers two through six and also a range of letters h through s so I can do an eye to make it case insensitive and a G to match every occurrence in the whole string.

  • So I'm gonna put that into my results.

  • So I'll do quote, sample that match to match this Reg X.

  • And here it's going to confident log the results.

  • So it's what happens.

  • And we have every letter and number between those ranges.

  • We've talked about how to match for specific characters, But what if you want to create set of characters that you do not want to match these air called negated character sets and you can create them with the carrot character?

  • So let me show you how to do that.

  • This we want to make a MME rejects that will match everything except all numbers and all vowels.

  • So we're going to put a opening close bracket, and I'm gonna put a carrot character that's right underneath a six on a keyboard and open zero through nine.

  • So we are not going to match all digits zero through nine and we're not going to match a e i o u.

  • And then I will put my flags here I d So this will match everything except zero through nine and a e i o u.

  • So to finish this up, I'm going to put quote sample dot match seminal pests in the rejects here and now I'll just run this and you can see we've returned everything except the vows and the numbers.

  • It includes the spaces and periods and punctuation also.

  • Sometimes you want a match if a character occurs one or more times.

  • If you want to do that, you can use the plus character.

  • So let me show you how to do that.

  • In this case, we're going to try to match in the word Mississippi whenever an S occurs one or more times.

  • So I'm just gonna put s plus and I'm gonna put a G to match it every time it occurs.

  • And if I run this, you'll see we have matched the first SS and then the second s s.

  • If they're just happened to be another s.

  • And here we would then match three times and you can see the third time only matches the single s because there's only one s in a row.

  • There is an option to match characters that occur zero or more times.

  • That's with this star character here.

  • So this rejects means match, Aggie and then matching 00 or more times.

  • So with this word goal, it's going to return g o.

  • And then every Oh, after it.

  • And then here, gut feeling There's a G with no goes after it, incidents matching zero or more times.

  • It's going to match that and just return G.

  • Now this one has No, it hasn't owes what doesn't have a G.

  • So it's gonna return.

  • No, it's not gonna match at all.

  • So down here we're going to do some coding and match a zero or one time.

  • So I'm going to change the change word here, and we always want to get the capital a and then for the lower case A.

  • We're going to do it a ass trick so it can match this whole thing of A's here.

  • So if I just do a council that log here and the result.

  • It should match all of the AIDS.

  • It worked.

  • I am going to talk to you about greedy and lazy matches.

  • A greedy match finds the longest possible part of the string that fits the Reg X pattern and returns it as a match.

  • A lazy match finds the smallest possible part of the string and returns that Reg Ex patterns default to greedy.

  • Let me show you what I mean.

  • Let's look at the string here Titanic, and we'll look at this rejects to find something in the string Titanic.

  • First we're looking for the letter T.

  • And then this part means after the letter T, we went zero or more of any letter.

  • So are this Means any letter A through Z, the Astra car star means zero arm or occurrences of this a through Z.

  • And then finally, we're looking for a letter I at the end.

  • So when we run this match here, it's going to do a greedy match and find this t i t.

  • A n I.

  • So it's going to start with A T is going to have zero or more letters and in with an I.

  • However, if we put a question mark here, it's going to be a lazy match.

  • So it's going to start with the tea and then remember, this is zero or more occurrences.

  • So instead of having all these letters were gonna go to the zero occurrence and not have any letters and then in with this I hear so it's just gonna be t I sew a greedy match would be t i t a n I a lazy match would just be t I.

  • So let's do the same thing down here.

  • We have this string here, which is a bit of HTML with an H one tag, some text Winter's coming and a closing each one tag.

  • If I run this now you'll see in the console.

  • It's going to return the whole thing.

  • So we're gonna start with the more than sign here, and then we have the dot, which is a wild card character.

  • So this means any letter or character and then the star or ass trick means zero or more occurrences.

  • So this means zero arm or occurrences of anything.

  • And then we end with the greater than sign.

  • So we're gonna start with the less than sign right here.

  • This is to open an HTML tag, and then we have the dot, which is a wild card the ass trucker star, which means zero or more occurrences.

  • So when you have a dot ass trick, that means zero or more of occurrences of any character and then end with the more than sign or that the closing tag here.

  • So it's going to go from here, and that's gonna have zero or more occurrences of any character all the way to this final character here this greater than sign.

  • So what we're going to do is change this.

  • So instead of instead of getting this whole thing, which was which is greedy, we want to do a lazy match where it starts here and just ends at the first occurrence of this greater than sign.

  • So all I have to do for that is to add a question mark.

  • So if I run this now, you'll see in the console.

  • It just has the opening H one tag.

  • This is a basic Reg X challenge.

  • So here is the scenario.

  • Ah, group of criminals escape from jail and ran away but you don't know how many.

  • However, you do know that they stay close together when they're around other people.

  • You are responsible for finding all the criminals at once, so the criminals are represented by a C.

  • So you can see here we have these three C's together, these air, all the criminals.

  • So we need to write a Reg X function that's going to match any times there's there is one or more sees in the string that's past it.

  • So this is how you would do it.

  • It's actually pretty simple.

  • We're just going to put a C and A plus.

  • So it's going to match one or Maur sees.

  • And if we load that, you'll see that it matches the CCC and we solve the challenge.

  • You can match patterns that air on Lee at the beginning of a string.

  • Previously, we talked about how the carrot character inside a character set is used to create negated character set.

  • Well, if you're not in a character set, if you're not in brackets, you can use the carrot character that looks like this to Onley match at the beginning of a string.

  • So here I'm just going to match the word cow if it's at the beginning of a string.

  • So I load this.

  • You can see True because it is the Cal is at the beginning of the string.

  • However, if I take the word Cal off here and put it in the middle here and I reload that again, it's going to say false because it does not match any more.

  • Since Cal is not at the beginning of the string, a carrot matches a beginning string pattern.

  • A dollar sign matches an ending string pattern.

  • So if we want to match the word caboose at the end of the string, here will tie pin into the Rejects will type in the Bruce and then up a dollar sign.

  • If you remember when we were matching a beginning string pattern, we put the care at the beginning.

  • But when we're matching an ending stream pattern, we put the dollar sign at the end.

  • So if I run this, we'll see true, because it's at the end of the string.

  • Now, if I actually move this over here and I run this, we'll see false.

  • It's no longer at the end of the string, so it no longer matches caboose.

  • Earlier, I showed you that this with the brackets and a through Z in it would match any letter A through Z.

  • There's something called a shorthand character class that's used to match a a wide range of things.

  • And there's one called Slash W that you can use to match the alphabet.

  • So if I put slash W here, that's gonna match capital A through Z, lower case a through Z, the digits zero through nine and underscore.

  • So just doing slash W is just a short hand way of pretty much matching any letter or number plus underscore.

  • If I put a G after this, that will make it match every occurrence.

  • So if I load this up, you'll see that it returns 31 because we're getting the length.

  • The length of this whole string, excluding the spaces in the period, is 31 there.

  • 31 letters in this string slash W matches all letters or numbers, but sometimes you want the opposite.

  • Sometimes you want to match everything that's not a letter or number.

  • So in this code we want to just match everything.

  • That's not a letter or number, which are the spaces and the period in the string here.

  • So instead of a slash lower case W we're going to use a slash uppercase W.

  • And then I will put a G to match every single occurrence, and we're gonna get the length here.

  • So if I run that we get a length of six, they're six characters.

  • That's those other spaces and the period that air, not letters, numbers or underscores.

  • There is a shorthand character class toe on Lee match numbers or digits, and it's slash D.

  • So if I put slash d here, it's going to match the digits here, which is the 50 and zero to match every single one.

  • I'm gonna put a G at the end for global and now the length should be three.

  • So if I run that, we'll see three in the console.

  • Sometimes you want to match all non numbers.

  • If you remember this slash d, a lower case D would match all numbers and the G made it match every occurrence of a number.

  • So if we change this to end uppercase d, this is going to match all non numbers.

  • So that's gonna be every single character in this whole string except the 50 and zero.

  • So we'll run this to get the length of that, and we get 24.

  • We're going to create a Reg X that can be used as a user name validator.

  • In this case, there are three requirements for the user name.

  • So here's the first requirement.

  • If there are numbers, they must be at the end.

  • Second requirement letters can be lower case and upper case, and then the third requirement is that it must be at least two letters long to letter names can't have numbers, which is kind of 1/4 requirement.

  • So let's create the Reg X here.

  • So first, we're going to do the requirement that the numbers should be at the end.

  • So if the numbers should be at the end, then letters should be at the beginning.

  • So to specify what's at the beginning, we're going to use the carrot sign, and then we're going to specify the letters that could be at the beginning, which are lower case and upper case letters.

  • So do a through the upper case and then, eh, a through Z lower case.

  • So now it shows that at the beginning is going to be a through Z uppercase or a through Z lower case.

  • And to specify the number of letters that are gonna be at the beginning, we're going to use the following.

  • I'm gonna buy a curly brace.

  • Two comma in curly brace.

  • The curly braces indicate the number of times the previous thing can match.

  • So the previous thing are these letters and these can match anywhere between two and infinite number of times.

  • So the the number before the comma is the minimum number of matches, and the number after the comma is the maximum number of matches.

  • So we know that every user name is going to start with at least two letters.

  • And since there's no number after the comma, it can have an infinite number of letters in the user name.

  • If we wanted to have at Max eight letters than we could put the number eight after the comma.

  • But since we don't care about the max, we're gonna leave.

  • It has nothing after the comma.

  • So next we want to show that the numbers must be at the end and you can have any number of numbers so I'll put slash the and that represents all numbers.

  • I'll put a star So this star or ass trick means that there should be zero or more.

  • So this means zero or more numbers.

  • And now to show that the zero are more numbers are at the end, I will put a dollar sign.

  • We can now run this rejects to make sure a user name meets all of these requirements.

  • We can match white space characters with slash s So slash s is going to match a space A cares return of Taba form feed and a new line character.

  • So if I add a G for global and run that we will see an array of all the spaces in that sentence, the white space is important in separating words sentence.

  • Now I'll show you how to match all non white space characters first, make a guess.

  • So remember, this is how you match whitespace characters.

  • And if you've been noticing the pattern so far, you'll realize to match non white space characters is just a capital s.

  • So if we run that you'll see an array of every character.

  • That's not a space you can specify the lower and upper number of pattern matches with quantity.

  • Specify IRS quantity, specify Chairs are used with curly brackets.

  • I actually talked about these a little bit before you put two numbers between the curly brackets for the lower and upper number of patterns.

  • So let's say I want to match anything any.

  • Oh, no.

  • Where the number of h is is between three and six.

  • H is.

  • So what I'll do is type O.

  • No, But after the h, I'm gonna put these curly brackets and put three comma six.

  • So this means we're gonna always match the letter o.

  • And then we're gonna match the letter H.

  • But we Onley match.

  • If there is in between three and six, each is and there will also match this space.

  • And, oh, if we had not had a number here and we just had three comma and the nothing in the in curly brace, then it would match for an infinite number of occurrences.

  • So it be three h is through an infinite number of ages, but we'll just leave at six.

  • Here's another example with quantities, festive fires where we're going to match four or more of the letter Z.

  • So I'll put the letters e and then the curly brackets.

  • I'll put four comma and then there will be nothing after the comma because, like I said before, when you have nothing after the Kama, there's an infinite number that I could go to, so it has to be at least four, and then it could be an infinite number of disease.

  • We can use quantities, specify IRS to specify an exact number of matches instead of just a lower and upper bound.

  • So if we want to search for the word timber with exactly four EMS, you could do it like this.

  • T i m.

  • And then I'll do the curly braces and then just put the number four and then b e r.

  • So remember before we had the up the lower bound than comma upper bound.

  • But if we know we want exactly four EMS, we can just put the number four in the curly braces.

  • You can specify the possible existence of an element with a question mark.

  • This checks for zero or one of the preceding element.

  • It's kind of like saying that the previous element is optional.

  • For example, their slight differences in American and British English.

  • So the word favorite can be spelled with Justin Oh, or with an O.

  • U with British English.

  • So let's create a reject set that searches that could match both forms of the word favorite.

  • So du fait votre papa, you with a question mark r i p e So this is this is saying that it may have a U or may not have a you, but all the other letters have to be the same.

  • Look ahead.

  • Zehr patterns that tell javascript to look ahead in your string to check for patterns further along.

  • This could be useful.

  • When you want to search for multiple patterns over the same string, there are positive Look, a heads and negative Look a heads.

  • So let me show you how they were.

  • So this right here is a positive look ahead.

  • So we have parentheses and then we have a question mark.

  • So the look ahead always start with a question mark, and then it's gonna be equals for a positive look ahead or an exclamation point for a negative look ahead.

  • So let's look back at the positive.

  • Look ahead.

  • So question mark equals means a positive look ahead and you.

  • So it's first going to check for the Q.

  • And it's going to look ahead to make sure there is a you later in the string.

  • And if there is a you late in the string will return just the queue.

  • So when we run this rejects on the quit string, it's going to return que because it finds the U later in the string.

  • But it doesn't return that you a negative look ahead.

  • It's going to make sure that something is not later in the string.

  • So here is going to search for a Q and make sure there's not a you later in the string.

  • And if there is not a you later in the string, it's going to return this.

  • So when we run this look ahead on the no quit screen string, it's going to find the Q.

  • It's goingto look ahead and find out there is no you since there is no you.

  • It's just going to return the Q because that's what we have before the parentheses and is going to return the queue here.

  • A more practical use of look ahead is to check two or more patterns in one string.

  • So down here in this example, we're going to use look heads in the P W rejects to match passwords that are greater than five characters long and have two consecutive digits.

  • So for a greater than five characters, we'll do parentheses.

  • And then it's always gonna start with a question mark equals if it's a positive look ahead and I want greater than five characters, so slash W would be all numbers and letter characters, and we want there to be five characters, at least five characters, and then we also want to consecutive digits.

  • So I'm going to make another look ahead question mark equals, and we're gonna say there could be any number of characters that are not digits.

  • This Capital B the slash Capital B is, ah, character.

  • That's not a digits, and the gastric means they're zero or more catchers that air.

  • Not a digit, but we also want to make sure that there are two or more digits.

  • So we have the slash D to say that they're gonna be digits, and then we have to sit me two or more digits.

  • So this is going to match for five or more characters and then two or more digits.

  • You can group things you are searching for inner rejects with parentheses.

  • So here we have, ah, parentheses around here, so we are searching for any number of characters.

  • And then after that, there's going to be a space.

  • You can also use these parentheses to repeat sub strings.

  • So this is called a capture group, and you can repeat this group any number of times.

  • So in this Red Jax, we have this first group in parentheses and we can refer.

  • We can repeat that first group with a slash one.

  • If we had another group with parentheses, we could repeat that second group with slash, too.

  • So in this case, we are searching for any group of any number of letters, followed by a space followed by any number of letters that air the same letters as before.

  • Because we're doing slash one.

  • If we test this rejects against this string is going to return true because it has any number of letters.

  • It has a space, and then it repeats any number of letters, the same letters from before.

  • So that's any number of letters.

  • The space, and then we're repeating the same notes letters as before.

  • Now here what we did slash one.

  • We could have just put this whole thing again, the parentheses with the slash w in a copy in peace here, so this would have been the same thing.

  • But instead of repeating this twice, we could just put slash one to save space.

  • So we don't have to repeat if we want to repeat the same search over and over again in a in a Reg X.

  • So if we look down here for the match at first, this may seem confusing.

  • What is returning but what it's returning The first element in the array is the full match.

  • So that's the the the number of characters, plus a space, plus any number of the same characters.

  • So the full matches this and then the second element in the array is just whatever is in the capture group.

  • This is the Capture group, and we, in what's in the capture group, is just rejects.

  • That's why we have rejects rejects, which is the full Reg X and then the SEC.

  • Elma of the Racist rejects, which is just the capture group the thing in the parentheses here.

  • So down here we're going to attempt to use capture groups to match numbers that are appeared on Lee three times in a string each separated by a space.

  • We want this Reg X to match something like this.

  • 42.

  • Space 42 Space 42.

  • We would also want to match something like 1 36 space 136 space 1 36 It doesn't matter what the number is, and we on Lee went to match a group of three, so we don't want to match four numbers.

  • Find number 600 just a group of three numbers separated by spaces.

  • So let's see how to do that.

  • We're going to use a capture group, so I'm going to put a parentheses slash D plus that mean any number of digits and that's gonna be the end of our capture group.

  • And then it's going to have a space, and then I'm gonna put slash one, which just is a shorthand way of repeating the first thing in parentheses, which is this and then we'll have another space in another slash one.

  • So this is going to look for any number in a space, any number in a space, any number.

  • So it's the number three times.

  • We're not quite done yet, though, because remember, I say said We want to match on Lee three numbers.

  • So this rejects would also match this if the number was in there four times because it would find 42 42 42 then stop and it would say, Oh, we found it but we don't want to match it.

  • If there's 4 42 days in a row, we only want to match it.

  • We only want this return.

  • Truth.

  • There's 3 42 in a row, so that's simple to fix.

  • We just have to have a bracket or I mean a carrot at the beginning and a dollar sign at the end.

  • So remember a cure.

  • It means that we want this at the beginning of the string and then a dollar sign means we want this at the end of the string.

  • So getting the care and the dollar sign just means we want exactly this.

  • We don't want anything before this.

  • We don't want anything after this.

  • So now this will return true for any group of three numbers.

  • Not a group of four numbers, Not a group of five numbers.

  • You can search and replace texts in a string using the replace function on a string.

  • The inputs for a place is first, the Reg X pattern you wanna search for.

  • And the second parameter is the string to replace the match or a function to do something.

  • So let's look at this example.

  • We have this text, the sky, It's silver.

  • We're going to search for silver.

  • This air rejects pattern.

  • So we're going to do a replace.

  • You got the wrong text.

  • Stop Replace.

  • We're gonna pass in the rejects the Silver Rejects, which is searching for the word silver and is going to replace it with the word blue.

  • So this is going to return.

  • The sky is blue now.

  • You can also access capture groups in the replacement string with dollar signs.

  • Let me show you what I mean down here.

  • So here we have a string code camp, and they're gonna call the replace function on that string.

  • Now, look at the rejects.

  • So appear we just used the variable silver rejects which was set to this rejects.

  • Here.

  • We're putting them in the rejects directly into the function instead of using a variable.

  • So look at this rejects.

  • We have this capture group, which just means any number of letters.

  • Then we have a space, and then we have this other capture group, which just means any number of letters.

  • So that's gonna find this code space camp, any number of letters, space, any number of letters.

  • So let's look here.

  • So up here we just put in the world we want to replace with, but here we do something different.

  • It says dollar $2 sign one.

  • So dollar sign to means the second Capture group, the second parentheses in the Reg X.

  • So the second Capture Group is this which has matched to the word camp.

  • So we're putting camp at the beginning, and then a space and then dollar sign.

  • One means the first capture group, which is this section which matched the word code.

  • So this is away so you can see a returns camp code, so we pass in the text code camp and it returns camp code.

  • So that's another cool thing you could do with capture groups.

  • When you're doing a replace So let's get back to the easier one up here.

  • We're going to actually make some changes down here that follows this pattern up here.

  • So this sandwich is good.

  • Well, we're going to find the word good here, and we're going to replace it with the word Okey dokey.

  • So here it's going to find the fix Reg X, which is good.

  • And it's gonna replace it with the replace text, which is Okey dokey.

  • This is a Reg Ex coding challenge.

  • The challenge is to write a Reg X and use the appropriate string methods to remove white space at the beginning and end of this string here.

  • Now the trim method would work here, but the challenge is to do it with just using regular exp oppressions.

  • So we are going to do a replace.

  • But we have to write a regular expression that's going to figure out what we're gonna replace.

  • Well, let's write a regular expression that finds all the space at the beginning and all the space at the end.

  • So this is how we'll do this to find everything at the beginning is going to start with a carrot which finds something at the beginning of a string and will put slash s which is all the white space and the any number of white space characters will do a plus.

  • Now we also want to find all the space of the end.

  • So I'll do a pipe character, which means or so all the space of the beginning or all the space at the end for all the space of the end will do slash s plus and then a dollar sign, which means this has to be at the end.

  • And so we find both the space of beginning and the spaceship end.

  • I'm going to add the flag g here don't.

  • Now we've found all the spaces.

  • We just have to finish this.

  • Replace here, so hello dot Replace and then in parentheses.

  • First the Rejects W s Reg X, and then we're going to replace all the spaces that's gonna find with just an empty string.

  • So this will effectively remove the spaces from that strength.

  • Uh oh.

  • I need a parentheses here.

  • And let's test this out.

  • You can see in the console Hello world with no spaces at the beginning or the end.

  • Congratulations on finishing this regular expressions course.

  • Well, keep learning.

hello and welcome to this course on regular expressions.

字幕與單字

單字即點即查 點擊單字可以查詢單字解釋

A2 初級

學習正則表達式(Regex)--初學者速成班。 (Learn Regular Expressions (Regex) - Crash Course for Beginners)

  • 8 0
    林宜悉 發佈於 2021 年 01 月 14 日
影片單字