Matching Numbers in JavaScript Strings using Regular Expressions

Learn how to match number patterns such as phone numbers and credit cards in the unstructured text using JavaScript Strings This video is a follow-up video for Search a String by matching against a regex in JavaScript

In this video, we will extract phone numbers and credit card data using regular expression and String.match available in JavaScript

Code

const data = ["Jesenia Downard",
"200-677-0045",
"4716 3558 2962 8986",
"Phylis Sansom",
"434-642-3827",
"4532 0252 3274 0270",
"Chrissy Parsley",
"620-637-7861",
"3766 9798 5170 2343",
"Yoshiko Bernardi",
"936-433-5071",
"5186 4586 2667 8788",
"Myrtle Yoon",
"698-349-7435",
"3461 6706 6163 3320",
"Benny Coppage",
"844-291-9937",
"5361 6979 7003 8092",
"Hilde Garvin",
"275-219-1054",
"3791 5243 6221 2833",
"Liberty Goucher",
"278-451-7877",
"4539 1831 8210 9047",
"Nannie Lipka",
"515-274-7232",
"4916 5089 1053 5522",
"Lindsy Salais",
"241-378-3493",
"3764 9429 3142 8329"]
// Matching Phone Numbers
data.forEach(entry => {
console.log('entry => ', entry);
console.log('matching phone number')
let result = entry.match(/[0-9]{3}-[0-9]{3}-[0-9]{3}/g)
console.log('result => ', result)
console.log('-'.repeat(20))
})
// Matching Credit Cards
data.forEach(entry => {
console.log('entry => ', entry);
console.log('matching phone number')
let result = entry.match(/[0-9]{4}\s[0-9]{4}\s[0-9]{4}\s[0-9]{4}/g)
console.log('result => ', result)
console.log('-'.repeat(20))
})

Full Transcript

0:00 Hey there, it's Harit Himanshu from bonsaiilabs. Welcome to the series on JavaScript. Today in this video, you will learn about how to find a number pattern inside the unstructured text using regular expressions in JavaScript. 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. That video will give you a good idea on why searching for a pattern inside a text using regular expression makes a good sense. In the previous video, we looked at the dataset which contained three key pieces of information, name, phone number, and credit card. 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. Now we know that even our phone numbers contain a pattern, but what kind of pattern?

0:48 It contains three numbers followed by a dash, followed by three numbers, followed by a dash again and finally followed by four numbers. So how do we create this pattern? As before we need a character set to define the range of numbers. Within the character set, we need to tell that the numbers may vary between zero and nine using the range notation. 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. 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. So we are saying, we are looking for exactly three numbers defined in the character set. Now as you may imagine, we can use this exercise again to complete our phone number pattern. 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.

1:44 But this time we are looking for exactly four numbers and this is how we can extract the phone numbers from our dataset. Let's run through the code to make sure that our understanding is correct. 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. 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. And since we do not want to stop on the first occurrence, we will make this a regular expression as a global search. We will pass this regular expression object to string dot match method and whatever results we get, we print that on the console. Let's run it. 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.

2:29 Now we know that even our credit card has a pattern. 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. So let's write the pattern to extract the credit cards now. So we will say we need a character set pattern zero to nine with four numbers followed by space. And repeating the same pattern three times with zero to nine four letters and followed by a space between them. To recap, we are looking for four numbers followed by three spaces between them, denoted by backslash S. We will now go ahead, clear the screen and run the code again. 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. 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 that's it. I hope you learned something new today. If you liked the video, please hit the subscribe button on our channel to receive more engaging content about JavaScript every single week. Until then, keep learning new skills and apply them regularly to become an expert programmer. See you next time.