Regex Flashcards

1
Q

Alteration

A

You can search for multiple patterns using the alternation or OR operator: |.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you test regex in JS

A

using test function as in:

fccRegex.test(myString)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are regex flags / modifiers and what are their functions?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a wildcard character

A

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./;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are character classes?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

match() function

A

The match() method retrieves the result of matching a string against a regular expression.

myRegex = /[a-z0-9]/gi
myStr.match(myRegex)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How to match negated characters sets?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the quantifier characters in regex?

A

? - match 0 on 1 times
* - match 0 or more times
+ - match 1 or more times
{#} - exact number of matches

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are common shortcodes?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are anchors and boundaries?

A
  • 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How to search for whitespace

A

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].

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Specifying upper and lower number of patterns

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are lookaheads and its types?

A

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 !@#$%^&
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

With what syntax do you reuse capture groups?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you search and replace text you match with capture groups?

A

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 ($).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Which meta characters need to be escaped with backslash escape?

A

|.?*+{}^$\/ (delimiter)
[]()|.\?*+{}\^$\\/

However, most meta characters lose their special value when they are within a character class.
As in:
[(][)][|][.][?][*][+][{][}][$][/]

Also remember to put the metacharacters as first or last of the character class.
[A-Za-Z0-9-]