Regular Expressions (Regex) in JavaScript
1. What is a Regular Expression?
A Regular Expression (Regex) is a pattern used to search, validate, and manipulate text strings in
JavaScript.
Common Uses:
1 • Email validation
2 • Password strength checking
3 • Phone number verification
4 • Extracting data from text
2. Regex Syntax in JavaScript
1 Literal Syntax (Recommended for Performance):
2 let regex = /pattern/;
4 Constructor Syntax:
5 let regex = new RegExp('pattern');
3. Important Symbols
1 . -> Any character
2 * -> 0 or more times
3 + -> 1 or more times
4 ? -> 0 or 1 time
5 ^ -> Start of string
6 $ -> End of string
7 \d -> Digit
8 \w -> Letter or digit
9 \s -> Whitespace
4. Example: Email Validation
Example Regex: /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-z]{2,}$/
1 ^ -> Start
2 + -> At least one character
3 @ -> Required symbol
4 \. -> Dot
5 $ -> End
5. Flags and Methods
1 Flags:
2 i -> Ignore case
3 g -> Global search
4 m -> Multiline
6 Methods:
7 [Link](string)
8 [Link](regex)
9 [Link](regex, newValue)