Regular expressions, often abbreviated as regex or regexp, are powerful tools in JavaScript (and many other programming languages) for pattern matching within strings. They allow you to describe a pattern of characters and perform operations like searching, matching, and replacing based on that pattern.
Regular expressions allow you to create flexible patterns to match strings. They are enclosed in slashes (/
) or created using the RegExp
constructor. Regular expressions can be simple, like /pattern/
, or complex, involving various characters and combinations.
`.`
(Dot): Matches any character except newline.
let dotPattern = /a.b/; // Matches 'aab', 'acb', but not 'ab'
`*`
(Asterisk): Matches 0 or more occurrences of the preceding character.
let asteriskPattern = /ca*t/; // Matches 'ct', 'cat', 'caat', 'caa...t', etc.
`+`
(Plus): Matches 1 or more occurrences of the preceding character.
let plusPattern = /ca+t/; // Matches 'cat', 'caat', 'caa...t', but not 'ct'
`?`
(Question Mark): Matches 0 or 1 occurrence of the preceding character.
let optionalPattern = /colou?r/; // Matches 'color' or 'colour'
[abc]
: Matches any character ‘a’, ‘b’, or ‘c’.
let charClassPattern = /[aeiou]/; // Matches any vowel
[^abc]
: Matches any character except ‘a’, ‘b’, or ‘c’.
let negCharClassPattern = /[^0-9]/; // Matches any non-digit character
[a-z]
: Matches any lowercase letter.
let lowercasePattern = /[a-z]/; // Matches any lowercase letter
^
(Caret): Matches the start of a string.
let startOfString = /^Start/; // Matches if the string starts with 'Start'
$
(Dollar Sign): Matches the end of a string.
let endOfString = /End$/; // Matches if the string ends with 'End'
\b
(Word Boundary): Matches a word boundary.
let wordBoundaryPattern = /\bword\b/; // Matches the whole word 'word'
()
(Parentheses): Groups patterns together.
let groupPattern = /(ab)+/; // Matches 'ab', 'abab', 'ababab', etc.
|
(Pipe): Acts as an OR operator inside a group.
let fruitPattern = /(apple|orange)/; // Matches 'apple' or 'orange'
let repeatPattern = /(\d{2})-\1/; // Matches '22-22', '33-33', etc.
i
(Case-insensitive): Enables case-insensitive matching.
let caseInsensitivePattern = /pattern/i; // Matches 'Pattern', 'PATTERN', etc.
g
(Global): Enables global matching (finds all matches).
let globalSearchPattern = /pattern/g; // Matches all occurrences of 'pattern'
m
(Multi-line): Enables multi-line matching.
let multiLinePattern = /^First/m; // Matches 'First' at the beginning of each line
(?=...)
: Positive lookahead.
let positiveLookaheadPattern = /x(?=y)/; // Matches 'x' only if followed by 'y'
(?!...)
: Negative lookahead.
let negativeLookaheadPattern = /x(?!y)/; // Matches 'x' only if NOT followed by 'y'
(?<=...)
: Positive lookbehind.
let positiveLookbehindPattern = /(?<=x)y/; // Matches 'y' only if preceded by 'x'
(?<!...)
: Negative lookbehind.
let negativeLookbehindPattern = /(?
*?
, +?
, ??
: Matches the smallest possible part.
let lazyQuantifierPattern = /a.*?b/; // Matches the shortest 'a...b' sequence
let emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;
let strongPasswordPattern = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/;
Regular expressions are a versatile tool for string manipulation in JavaScript. By mastering the basics and exploring advanced techniques, you gain the ability to craft intricate patterns for various tasks. Remember to test your regular expressions thoroughly, and with practice, you'll become proficient in leveraging this powerful feature. Feel free to experiment with different patterns and scenarios to deepen your understanding of regular expressions. Happy coding !❤️