Regex Flashcards
Alteration
You can search for multiple patterns using the alternation or OR operator: |.
How do you test regex in JS
using test function as in:
fccRegex.test(myString)
What are regex flags / modifiers and what are their functions?
g - (global) - this changes how the regex expressions is applied to the input string. Other modifiers change how the input is interpreted. By default engine will try to give the first match. Global modifiers disables it - returns all matches.
i - (case insensitive) might not recognize not ascii letters.
m - Multiline mode (changes behaviour of anchors ^ $, flag “m”). It makes them match against each line of the input instead the complete input string.
s - Enables “dotall” mode, that allows a dot . to match newline character \n (covered in the chapter Character classes).
x- (extended) - great way to increase the readability of the regex. Makes all whitespace and end of line in the pattern ignored. It also adds a #, which is like a comment.
u - Enables full unicode support. The flag enables correct processing of surrogate pairs. More about that in the chapter Unicode: flag “u” and class \p{…}.
y - “Sticky” mode: searching at the exact position in the text (covered in the chapter Sticky flag “y”, searching at position)
What is a wildcard character
The wildcard character . will match any one character. The wildcard is also called dot and period. You can use the wildcard character just like any other character in the regex.
let huRegex = /hu./;
What are character classes?
With character classes you can search with literal patterns.
Character classes allow you to define a group of characters you wish to match by placing them inside square ([ and ]) brackets
match() function
The match() method retrieves the result of matching a string against a regular expression.
myRegex = /[a-z0-9]/gi
myStr.match(myRegex)
How to match negated characters sets?
To create a negated character set, you place a caret character (^) after the opening bracket [ and before the characters you do not want to match.
For example, /[^aeiou]/gi matches all characters that are not a vowel. Note that characters like ., !, [, @, / and white space are matched - the negated vowel character set only excludes the vowel characters.
What are the quantifier characters in regex?
? - match 0 on 1 times
* - match 0 or more times
+ - match 1 or more times
{#} - exact number of matches
What are common shortcodes?
Alhpanumeric characters:
[A-Za-z0-9_] using \w followed by the global tag
if you want to extract the opposite: [^A-Za-z0-9_] \W
[0-9] shortcut: \d
[^0-9] shortcut: \D
\s any space character [\t\f\r\n]
You can combine shortcodes inside of a character class.
What are anchors and boundaries?
- Outside of a character set, the caret ^ is used to search for patterns at the beginning of strings.
- You can search the end of strings using the dollar sign character $ at the end of the regex.
- \b word boundaries (regex = /(\b[A-Za-z]+)/g will look for words with letters only applying word boundary on the results)
How to search for whitespace
You can search for whitespace using \s, which is a lowercase s. This pattern not only matches whitespace, but also carriage return, tab, form feed, and new line characters. You can think of it as similar to the character class [ \r\t\f\n\v].
Specifying upper and lower number of patterns
You can specify the lower and upper number of patterns with quantity specifiers. Quantity specifiers are used with curly brackets ({ and }). You put two numbers between the curly brackets - for the lower and upper number of patterns.
What are lookaheads and its types?
Lookaheads are patterns that tell JavaScript to look-ahead in your string to check for patterns further along. This can be useful when you want to search for multiple patterns over the same string.
There are two kinds of lookaheads: positive lookahead and negative lookahead.
Lookaheads shine not only in pattern recognition but password validations. (?=.*[a-z]) positive lookahead will ask at least one lower case letter while negative (?!.*[\W]) will not allow non-word signs such as !@#$%^&
With what syntax do you reuse capture groups?
You can search for repeat substrings using capture groups. Parentheses, ( and ), are used to find repeat substrings.
To specify where that repeat string will appear, you use a backslash () and then a number.
let reRegex = /^(\d+)\s\1\s\1$/;
How do you search and replace text you match with capture groups?
You can search and replace text in a string using .replace() on a string. The inputs for .replace() is first the regex pattern you want to search for. The second parameter is the string to replace the match or a function to do something.
“Code Camp”.replace(/(\w+)\s(\w+)/, ‘$2 $1’);
You can also access capture groups in the replacement string with dollar signs ($).