See you next time.In this article you will learn how to match numbers and number range in Regular expressions. Until then, keep learning new skills and apply them regularly to become an expert programmer. If you liked the video, please hit the subscribe button on our channel to receive more engaging content about JavaScript every single week. So as you can see, you can use the string dot match in combination with regular expressions to extract the information out of an unstructured text. And now as you may imagine, we were not able to extract the name and the phone numbers, but we were able to extract the credit card numbers just fine.
We will now go ahead, clear the screen and run the code again. To recap, we are looking for four numbers followed by three spaces between them, denoted by backslash S. And repeating the same pattern three times with zero to nine four letters and followed by a space between them. So we will say we need a character set pattern zero to nine with four numbers followed by space. So let's write the pattern to extract the credit cards now. It has four numbers followed by a space followed by phone numbers, followed by space, followed by four numbers a space and then finally four numbers. And now as we run that we will see that we are extracting our phone numbers and it is not matching on names and the credit cards.Ģ:29 Now we know that even our credit card has a pattern. We will pass this regular expression object to string dot match method and whatever results we get, we print that on the console. And since we do not want to stop on the first occurrence, we will make this a regular expression as a global search. We'll say we need zero to nine with exactly three numbers followed by dash with zero to nine three numbers again and then zero to nine followed by four numbers again. So we are using the same dataset as in the previous video and we are going to add the regular expression for extracting phone numbers. Let's run through the code to make sure that our understanding is correct.
First, we will add a dash and then repeat the previous pattern since it exactly matches it and then we will add another dash followed by the similar pattern.ġ:44 But this time we are looking for exactly four numbers and this is how we can extract the phone numbers from our dataset. Now as you may imagine, we can use this exercise again to complete our phone number pattern.
So we are saying, we are looking for exactly three numbers defined in the character set. We will add a pair of curly brace and within that pair we tell exactly how many occurrences of that preceeding expression to look for.
But how do we tell that there must be exactly three numbers? For that, we will use a special characters that are available in regular expressions. Within the character set, we need to tell that the numbers may vary between zero and nine using the range notation. So how do we create this pattern? As before we need a character set to define the range of numbers. Now we know that even our phone numbers contain a pattern, but what kind of pattern?Ġ:48 It contains three numbers followed by a dash, followed by three numbers, followed by a dash again and finally followed by four numbers. We also learned how to extract the name from the dataset and this video we will look at how to extract the phone numbers and credit card information. In the previous video, we looked at the dataset which contained three key pieces of information, name, phone number, and credit card. That video will give you a good idea on why searching for a pattern inside a text using regular expression makes a good sense. If you have not watched my previous video on how to find a string pattern using regular expression, click on the card above and watch that one first before resuming this video. Today in this video, you will learn about how to find a number pattern inside the unstructured text using regular expressions in JavaScript. 0:00 Hey there, it's Harit Himanshu from bonsaiilabs.