Loading
Lesson 17
Courses / JavaScript for Complete Beginners
String Replacement in JavaScript

Video Transcript

Welcome to NDK Attack World. In this lesson we are going to learn how to do string replacement. That is, given a string of characters, we are going to learn how we can find some pattern of characters and then replace that with another set of characters. So for that first we're going to need a string. I'm just going to use that previous example of the Hello World string. In this video I'm using the Firefox console, okay, Mozilla Firefox Browser console. So given you have a string with two words Hello World like this, suppose I want to replace the word World with another word. Let's replace it with Earth. So to do that we're going to use a method, a function called Replace. So you call that method on the string itself with a dot and then replace, okay. So this is the name of the method. Remember, method is another name for a function. It's a special type of function because in this case this function is attached to a string. In this context we call it a method, okay, a method of the string type. So we're going to call replace. I always have the parenthesis, right, because we're calling a function. So replace takes in this case two arguments. The first one is going to be what we're looking for in the string. So we were looking for, we want to replace the word World. So let's write the World string. And with the second argument to the replace method you have to pass. What do you want this to be replaced with? So we want to replace the word World with something else. In this case we want Earth. So in this string right here it's going to search for the pattern W-O-R-L-D exactly as we mentioned here in the first argument. And it's going to take this pattern of characters and replace it with this other set of characters that you passed as a second argument to replace. It just so happens that it's a word and it's Earth. So let's see what we get. So the output of this method, replace, will create a new string. A new string with the word World replaced with Earth. So we get Hello Earth, a new string. Okay so that's the replace method. The first argument is the pattern to search for. And the second argument is what we want the pattern to be replaced with. Now there's a detail about this call here that we're going to see if we add more stuff to our original string. Suppose you have Hello World, Oh Dear World in this case. We have a string with more two instances of the word World. And then what happens if we try to call replace on this guy? Will both of them get replaced? Let's see. So what we get is Hello Earth, Oh Dear World. So the second word, World was not replaced. So the method replace on the string will only replace the first instance found. So it's going to start looking for the pattern W-O-R-L-D, the word World, and finds it right here. Then it's going to replace it with Earth. But then it stops because it only does it once. Okay? It only replaces the first instance it finds. Now if you really want to replace all the instances, not only this guy, but also all the other guys that come subsequently. So you have to do a little more work in your call to the replace method. So it's going to be almost the same thing except the first argument right here. So you're going to call the first argument instead of pressing just a string. You have to pass it in a special format for the language JavaScript. We're going to use instead of the quotes, we're going to use this forward slash. And then at the ending quote, another forward slash. Okay? So in JavaScript, when you have something between the forward slashes in this fashion, this is called a regular expression. Regular expression is some kind of pattern that you can devise and then replace will know that if it finds this pattern, it will replace of the word Earth. So we just got introduced to regular expressions. There's not much we don't know much about it. But just be aware that when you see these slashes and means, okay, this is treating this pattern as a regular expression. Okay? Now if you want to be able to replace every instance of world with Earth, you have to add this kind of option to the regular expression. We put a G there. So after the last closing slash, add a G, this G stands for global. So we're going to perform a global substitution. Okay, global replacement of every instance of the word world, we're going to replace with Earth. So let's try it out. Now you can see the first world becomes Earth and the second world becomes Earth and so on. If you had many, many, many, many worlds in the string, it would have been replaced with that. Let's see. There you go. Everything gets replaced. Every world word gets replaced with the word Earth. Just by using this G here after the regular expression with the pattern, which just so happens to be a word with the characters w, o, followed by r, followed by l, followed by d. Okay? So that's string replacement with the replace method of the string type in JavaScript. Thank you so much for watching and until the next time.
No comments yet (loading...)
No comments yet (loading...)
Did you like the lesson? 😆👍
Consider a donation to support our work: