Regular Expressions (RegEx) are special patterns used to search, match, validate, and manipulate strings in JavaScript.
They are widely used for:
- Form Validation
- Email Validation
- Password Validation
- Mobile Number Validation
- Search Operations
- Replace Operations
- Pattern Matching
Regular Expressions are one of the most important topics in JavaScript and are frequently asked in interviews.
A Regular Expression is a sequence of characters that forms a search pattern.
It is used to:
- Search text
- Match patterns
- Validate user input
- Replace characters
- Extract information from strings
var regex = /pattern/;Or
var regex = new RegExp("pattern");var regex = /hello/;
var str = "hello world";
console.log(regex.test(str));true
/hello/searches for the word "hello".- The string contains "hello".
- Therefore
test()returns:
true
var regex = /world/;
var str = "hello world";
console.log(regex.test(str));true
The word world exists in the string.
Hence:
true
var regex = /hi/;
var str = "hello world";
console.log(regex.test(str));false
The word hi is not present inside the string.
Therefore:
false
The test() method checks whether the given pattern exists in the string.
regex.test(string)- true → Pattern Found
- false → Pattern Not Found
The dot matches any single character.
var regex = /h.llo/;
var str = "hello world";
console.log(regex.test(str));true
The dot (.) replaces one character.
h . l l o
e replaces .
Hence:
hello → Match Found
The asterisk means:
Zero or More Occurrences
var regex = /h*llo/;
var str = "hllo world";
console.log(regex.test(str));true
h*
means:
h → 0 times
h → 1 time
h → many times
So:
hllo
hello
hhllo
hhhllo
All are valid matches.
The plus means:
One or More Occurrences
var regex = /h+llo/;
var str = "hhllo world";
console.log(regex.test(str));true
h+
means:
hllo
hhllo
hhhllo
hhhhllo
At least one h is mandatory.
var regex = /hello/i;
console.log(regex.test("HELLO"));true
The i modifier ignores uppercase and lowercase differences.
var str = "hello hello hello";
var regex = /hello/g;
console.log(str.match(regex));["hello","hello","hello"]
The global modifier searches all occurrences.
| Symbol | Meaning |
|---|---|
| . | Any one character |
| * | Zero or more occurrences |
| + | One or more occurrences |
| ? | Zero or one occurrence |
| ^ | Starts with |
| $ | Ends with |
| [] | Character set |
| [a-z] | Lowercase letters |
| [A-Z] | Uppercase letters |
| [0-9] | Digits |
| \d | Any digit |
| \D | Non-digit |
| \w | Word character |
| \W | Non-word character |
| \s | White space |
| \S | Non white space |
var email = "abc@gmail.com";
var regex = /^[a-zA-Z0-9]+@[a-z]+\.[a-z]{2,3}$/;
console.log(regex.test(email));true
var mobile = "9876543210";
var regex = /^[0-9]{10}$/;
console.log(regex.test(mobile));true
var password = "Java@123";
var regex = /^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9]).{8,}$/;
console.log(regex.test(password));true
Password must contain:
- One Uppercase letter
- One Lowercase letter
- One Number
- Minimum 8 Characters
| * Operator | + Operator |
|---|---|
| Zero or More | One or More |
| Empty value allowed | Empty value not allowed |
| h*llo matches "llo" | h+llo does not match "llo" |
| Optional occurrences | Minimum one occurrence |
Regular Expressions are used in:
- Email Validation
- Password Validation
- Mobile Number Validation
- Username Validation
- Search Functionality
- Find and Replace
- Data Extraction
- Form Validation
- Parsing Text Files
- Input Sanitization
Answer:
Regular Expression is a sequence of characters used to create search patterns for matching, validating, and manipulating strings.
Answer:
test()It returns:
- true
- false
Answer:
It matches any one character.
Example:
/h.llo/Matches:
hello
hallo
hxllo
Answer:
*→ Zero or More Occurrences+→ One or More Occurrences
Answer:
It performs a case-insensitive search.
Example:
/hello/iMatches:
HELLO
Hello
hello
Regular Expressions are powerful tools used for pattern matching, validation, and string manipulation in JavaScript. They simplify complex searching operations and are extensively used in real-world applications such as form validation, email verification, password checking, and data extraction. Mastering Regular Expressions is an important skill for every JavaScript developer and a frequently asked topic in technical interviews.