Basic Regular Expression
/abc/
In JS, regexes are strings that begin and end with /
The simplest regexes simply match a particular substring
The above regular expression matches any string containing "abc"
● Matches: "abc", "abcdef", "defabc", ".=.abc.=.", ...
● Doesn't match: "fedcba", "ab c", "PHP", ...
Wildcards, Case sensitivity
A . matches any character except a \n line break
● /.ax../ matches "Faxes", "Jaxes", "Taxes", "maxie", etc.
A trailing i at the end of a regex (after the closing /) signifies a case-insensitive
match
● /cal/i matches "Pascal", "California", "GCal", etc.
Quantifiers: *, +, ?
- means 0 or more occurrences
● /abc*/ matches "ab", "abc", "abcc", "abccc", ...
● /a(bc)*/ matches "a", "abc", "abcbc", "abcbcbc", ...
● /a.*a/ matches "aa", "aba", "a8qa", "a!?xyz__9a", ...
- means 1 or more occurrences
● /Hi!+ there/ matches "Hi! there", "Hi!!! there!", ...
● /a(bc)+/ matches "abc", "abcbc", "abcbcbc", ...
? means 0 or 1 occurrences
● /a(bc)?/ matches only "a" or "abc"
Character ranges: [start-end]
Inside a character set, specify a range of characters with -
● /[a-z]/ matches any lowercase letter
● /[a-zA-Z0-9]/ matches any lowercase or uppercase letter or digit
Inside a character set, - must be escaped to be matched
● /[+-]?[0-9]+/ matches an optional + or -, followed by at least one digit