Regular Expressions
Basic Special Characters
/ - Regexes are enclosed within forward slashes to indicate where they begin and where they end
"^" - The circumflex accent matches the beginning of the string or the beginning of the line. Your pattern will not match anything that has any text before if.
Example:
/^Once upon a time/
will match any line beginning with that sentence
$ - The dollar sign matches the end of the string. The opposite of the ^
Example:
/ation$
will match any line ending with the string "ation"
[] - Brackets are used to match a single character from the list enclosed in them. Regexes accept ranges of letters such as a-z or d-m, and even ranges like [a-f0-6] (a to f and zero to 6)
Example:
/[abc]/
will match either a,b or c
"*" - The asterisk acts as a quantifier to specify that the preceding character may appear 0 to n times.
Example:
/[ab]\*/
will match abab, aaaaaaa but will also match an empty string
+ - Plus sign is similar to
*
, but the preceding character has to appear at least once, it will match the same strings as the previous example, except for the empty string{} - The braces are used when you want to specify the number of times the preceding character should appear. Writing a single number within braces will match the strings that contain the preceding character or pattern that exact amount of times. Braces also include ranges. If you want a string that contains 4 to 8 characters use
{4,8}
Last updated