JavaScript RegExp (Regular Expressions)
Regular Expressions (RegExp) in JavaScript allow pattern matching and text manipulation. They are commonly used for validating input, searching, and replacing text.
1️⃣ Creating a Regular Expression
There are two ways to create a RegExp in JavaScript:
1.1 Using Literal Syntax (Recommended)
1.2 Using the RegExp Constructor
2️⃣ Flags in Regular Expressions
Flags modify how a regex behaves:
Flag | Meaning |
---|---|
g | Global search (find all matches, not just the first one) |
i | Case-insensitive search |
m | Multi-line search |
s | Allows . to match newlines |
u | Enables Unicode support |
y | Sticky search (matches from last index) |
Example:
3️⃣ Common RegExp Methods
3.1 test()
– Check if Pattern Exists
Returns true
if the pattern exists in the string, else false
.
3.2 match()
– Get Matches
Returns an array of matches or null
if no match is found.
3.3 search()
– Find Position of First Match
Returns the index of the first match or -1
if no match is found.
3.4 replace()
– Replace Matching Text
With a global flag (g
):
3.5 split()
– Split a String Using a Pattern
4️⃣ Regular Expression Patterns
4.1 Character Classes
Pattern | Meaning | Example |
---|---|---|
\d | Matches a digit (0-9) | /\d/ → "abc123" → ["1"] |
\D | Matches a non-digit | /\D/ → "123abc" → ["a"] |
\w | Matches a word character (a-z, A-Z, 0-9, _) | /\w/ → "Hello_123" → ["H"] |
\W | Matches a non-word character | /\W/ → "Hello!" → ["!"] |
\s | Matches whitespace (space, tab, newline) | /\s/ → "hello world" → [" "] |
\S | Matches non-whitespace | /\S/ → " " → No match |
4.2 Quantifiers
Quantifier | Meaning | Example |
---|---|---|
+ | One or more times | /\d+/ → "123abc" → ["123"] |
* | Zero or more times | /\d*/ → "abc" → [""] |
? | Zero or one time | /colou?r/ → "color" → ["color"] |
{n} | Exactly n times | /\d{3}/ → "1234" → ["123"] |
{n,} | At least n times | /\d{2,}/ → "12345" → ["12345"] |
{n,m} | Between n and m times | /\d{2,4}/ → "12345" → ["1234"] |
4.3 Anchors
Anchor | Meaning | Example |
---|---|---|
^ | Start of a string | /^hello/ → "hello world" → ["hello"] |
$ | End of a string | /world$/ → "hello world" → ["world"] |
\b | Word boundary | /\bcar\b/ → "car is here" → ["car"] |
4.4 Groups and Alternation
Pattern | Meaning | Example |
---|---|---|
(abc) | Capturing group | /(\d+)-(\d+)/ → "123-456" → ["123-456", "123", "456"] |
(?:abc) | Non-capturing group | `/(?:hello |
` | ` | Alternation (OR) |
5️⃣ Examples of Using Regular Expressions
5.1 Validate an Email
5.2 Validate a Phone Number
5.3 Validate a Strong Password
A strong password must have at least 8 characters, one uppercase letter, one lowercase letter, one number, and one special character.
5.4 Extract Numbers from a String
Conclusion
JavaScript Regular Expressions (RegExp
) are powerful for text searching, validation, and manipulation. By mastering character classes, quantifiers, anchors, and groups, you can perform complex pattern matching with ease.
Let me know if you need more examples! 😊