Regular Expressions (RegExp)
A tiny pattern language for matching, validating and extracting text — the standard tool behind email checks, search boxes and form validation.
What you will learn
- Read a basic regex pattern
- Test and search strings with a regex
- Validate input with a real-world pattern
A pattern that describes the shape of text
A regular expression (regex for short) is a small pattern that describes the shape of text you are looking for — not an exact word, but a rule like "three digits, then a dash, then four digits". You use it to test whether text matches, find parts of it, or replace them. In JavaScript a regex is written between two slashes, like /cat/.
The simplest pattern just looks for exact characters. The .test() method answers a yes/no question: does this string contain a match?
<script>
const pattern = /cat/; // looks for the letters c-a-t in a row
document.write(pattern.test("the cat sat") + "<br>"); // true
document.write(pattern.test("a dog barked")); // false
</script>/cat/ is a pattern looking for "cat" anywhere in the text. pattern.test("the cat sat") finds it, so it returns true; the second string has no "cat", so it returns false. .test() is the quickest way to ask "does this match?".
Note: Output: true false
The handful of symbols that do the work
Regex power comes from a few special symbols that stand for "a kind of character" or "how many". Here are the everyday ones — learn these and you can read most patterns:
| Symbol | Means |
|---|---|
\d | Any digit 0–9 |
\w | Any letter, digit or underscore |
. | Any single character |
+ | One or more of the thing before it |
* | Zero or more of the thing before it |
{2,4} | Between 2 and 4 of the thing before it |
[a-z] | Any one character in the range a–z |
^ $ | Start / end of the string |
| | OR — either side |
Let us read a real pattern. This one checks an Indian-style 10-digit mobile number. Decoding it left to right: ^ means start, \d{10} means exactly ten digits, and $ means end — so the whole string must be exactly ten digits, nothing more.
<script>
const phone = /^\d{10}$/; // start, exactly 10 digits, end
document.write(phone.test("9876543210") + "<br>"); // true
document.write(phone.test("98765") + "<br>"); // false (too short)
document.write(phone.test("98765 43210")); // false (has a space)
</script>The ^...$ anchors force the pattern to cover the entire string. "9876543210" is exactly ten digits, so it passes. "98765" has only five digits, and "98765 43210" contains a space — both fail because they do not match "ten digits and nothing else". This anchoring is the key to strict validation.
Note: Output: true false false
A worked example: a simple email check
Form validation is the most common real use. We build a deliberately simple email pattern and read every piece. (Real-world email validation is famously tricky — a sensible simple check like this is enough for most forms.)
<input id="email" placeholder="Type an email">
<button id="check">Check</button>
<p id="out"></p>
<script>
// something @ something . something — no spaces
const emailPattern = /^[\w.]+@[\w]+\.[a-z]{2,}$/i;
document.getElementById("check").addEventListener("click", function () {
const value = document.getElementById("email").value;
const ok = emailPattern.test(value);
document.getElementById("out").textContent =
ok ? "Looks like a valid email ✅" : "Not a valid email ❌";
});
</script>Reading the pattern in plain words:
^[\w.]+— start with one or more letters, digits or dots (the part before the @).@— then a literal @ sign.[\w]+— then one or more letters/digits (the domain name).\.— then a literal dot (the\escapes it, so it means a real dot, not "any character").[a-z]{2,}$— end with at least two letters (like com, org, in). Theiflag after the closing slash makes it case-insensitive.
Note: Output: Type "asha@mail.com" → Looks like a valid email ✅ Type "asha@mail" → Not a valid email ❌ (no .something at the end) Type "asha mail.com" → Not a valid email ❌ (has a space, no @)
Tip: You do not need to memorise complex patterns. Understand the handful of symbols above, and for anything tricky, build and test your pattern on a site like regex101.com — every professional does this.
Q. In the pattern /^\d{10}$/, what do the ^ and $ ensure?
✍️ Practice
- Write a regex that tests whether a string is exactly 6 digits (a PIN code).
- Use
.test()to check whether a username contains only letters, digits and underscores.
🏠 Homework
- Build a small form that validates three fields — a 10-digit phone, a simple email, and a 6-digit PIN — each with its own regex, showing a tick or cross beside each.