A toll-free number is a telephone number that is free for the caller to dial. Calls to toll-free numbers are typically paid for by the company or organization that owns the number, rather than by the person making the call. Toll-free numbers are often used by businesses and other organizations as a way to provide customer support or to encourage people to call for more information about their products or services. In this article let’s understand how we can create a regex for SSN and how regex can be matched for a valid SSN.
Regex (short for regular expression) is a powerful tool used for searching and manipulating text. It is composed of a sequence of characters that define a search pattern. Regex can be used to find patterns in large amounts of text, validate user input, and manipulate strings. It is widely used in programming languages, text editors, and command line tools.
Structure of a Toll Free Number
A Toll Free Number should have the following criteria and structure-
- It should have 9-10 digits.
- It should begin with 8
- The next 2 digits can be 00, 33, 44, 55, 66, 77, 88
- This can be followed by 6 digits of valid numbers
Regex for checking if Toll Free Number is valid or not
Regular Expression-
/^(\+?1)?(8(00|33|44|55|66|77|88)[2-9]\d{6})$/gm
Test string examples for the above regex-
Input String | Match Output |
---|---|
1812121212 | does not match |
1800233423 | matches |
856-452-6789 | does not match |
844221122 | matches |
Here is a detailed explanation of the above regex-
/^(\+?1)?(8(00|33|44|55|66|77|88)[2-9]\d{6})$/gm
1st Capturing Group (\+?1)?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
\+ matches the character + with index 4310 (2B16 or 538) literally (case insensitive)
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
1 matches the character 1 with index 4910 (3116 or 618) literally (case insensitive)
2nd Capturing Group (8(00|33|44|55|66|77|88)[2-9]\d{6})
8 matches the character 8 with index 5610 (3816 or 708) literally (case insensitive)
3rd Capturing Group (00|33|44|55|66|77|88)
1st Alternative 00
00 matches the characters 00 literally (case insensitive)
2nd Alternative 33
33 matches the characters 33 literally (case insensitive)
3rd Alternative 44
44 matches the characters 44 literally (case insensitive)
4th Alternative 55
55 matches the characters 55 literally (case insensitive)
5th Alternative 66
66 matches the characters 66 literally (case insensitive)
6th Alternative 77
77 matches the characters 77 literally (case insensitive)
7th Alternative 88
88 matches the characters 88 literally (case insensitive)
Match a single character present in the list below [2-9]
2-9 matches a single character in the range between 2 (index 50) and 9 (index 57) (case insensitive)
\d matches a digit (equivalent to [0-9])
{6} matches the previous token exactly 6 times
$ asserts position at the end of a line
Global pattern flags
i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])
g modifier: global. All matches (don't return after first match)
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
Hope this article was useful to check if the string is a valid Toll Free Number or not. In this article, we explored the concept of toll-free numbers and their significance in customer support and communication. We also delved into the power of regular expressions (regex) as a versatile tool for pattern matching. Specifically, we examined how to create a regex pattern to validate Toll Free Numbers, breaking down the components and explaining the associated regex expressions. By understanding and applying regex, businesses can ensure accurate identification of valid Toll Free Numbers, enhancing their customer interactions and services.