Checking that a field is not empty is easy. Checking that it looks like an email address or a phone number needs a way to describe the shape of text. Regular expressions do that.
What a Regular Expression Is
A regular expression, often called a regex, is a pattern that describes a set of strings. In JavaScript it is written between slashes, as in /abc/. The method pattern.test(text) returns true if the text matches, and text.match(pattern) returns the matches or null.
Definition: Regular expression. A pattern written between slashes that describes the form that text must take.
Building Patterns
Special symbols make patterns flexible. \d matches one digit and \w matches a letter, digit or underscore. A dot matches any character. Square brackets match one character from a set, so [a-z] matches one lowercase letter. The symbol ^ marks the start of the text and $ marks the end. A quantifier says how many times the part before it may repeat: + means one or more, * means zero or more, ? means zero or one, and {10} means exactly ten.
Definition: Quantifier. A symbol such as +, * or {n} that says how many times a pattern part may repeat.
Table 4.3: Common regular expression symbols
| Symbol | Meaning | Example |
|---|---|---|
\d |
One digit | \d\d matches 42 |
[a-z] |
One letter from a set | [a-c] matches b |
^ and $ |
Start and end of text | ^abc$ |
+ |
One or more | \d+ matches 2024 |
{n} |
Exactly n times | \d{10} |
. |
Any one character | a.c matches abc |
Anchoring and Common Patterns
Without ^ and $, a pattern matches any part of the text. A ten-digit phone check without anchors would accept fourteen digits, because ten of them match. Wrapping the pattern in ^ and $ forces the whole text to match. A phone number is /^\d{10}$/. A simple email is /^[^\s@]+@[^\s@]+\.[^\s@]+$/, which needs text, an at sign, text, a dot and text. A password of at least eight characters with a letter and a digit is /^(?=.*[A-Za-z])(?=.*\d).{8,}$/.
Example 4.3: Testing phone, email and password patterns
const phone = /^\d{10}$/;
console.log(phone.test("9876543210"));
console.log(phone.test("98765"));
console.log(phone.test("98765abcde"));
const email = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
console.log(email.test("asha@example.com"));
console.log(email.test("asha@example"));
const pass = /^(?=.*[A-Za-z])(?=.*\d).{8,}$/;
console.log(pass.test("abc12345"), pass.test("abcdefgh"));
Output:
true
false
false
true
false
true false
Common mistakes
- Leaving out ^ and $, so that a pattern matches part of a longer text.
- Forgetting to escape the dot in an email pattern as \. when a real dot is meant.
- Writing an email pattern so strict that it rejects valid addresses.
- Treating a pattern as proof that an address exists, when it checks only the shape.
Key takeaways
- A regular expression describes the shape of text, and test returns true or false.
- Use ^ and $ so that the whole text must match.
- \d is a digit, [a-z] is a set, and quantifiers such as + and {10} say how many.
- A pattern checks the form of an email, and does not prove that the mailbox exists.
Practice questions
- Write a pattern for a ten-digit phone number.
- What do ^ and $ do?
- What does the quantifier + mean?
Answers to the practice questions
- /^\d{10}$/.
- They mark the start and the end of the text, so the whole text must match.
- One or more of the part before it.
Sources
- Official BCA syllabus (NEP 2025 scheme), Web Application Development, course code 25BCA405DS04, Unit IV
