Regular Expressions Flashcards

(138 cards)

1
Q

i

A

With this flag the search is case-insensitive: no difference between A and a.

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

g

A

With this flag the search looks for all matches, without it – only the first one

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

m

A

Multiline mode

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

s

A

“Dotall” mode, allows . to match newlines

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

u

A

Enables full unicode support.

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

y

A

Sticky mode

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

let str = “I love JavaScript!”;

alert( str.search(/LOVE/i) ); //

alert( str.search(/LOVE/) ); //

A

2 (found lowercased)

-1 (nothing found without ‘i’ flag)

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

let str = “A drop of ink may make a million think”;

alert( str.search( /a/ ) );

A

15

first “a” is 15 and it’s case sensative

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

\d

A

A digit: a character from 0 to 9.

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

\s

A

A space symbol: that includes spaces, tabs, newlines.`

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

\w

A

A “wordly” character: either a letter of English alphabet or a digit or an underscore. Non-Latin letters (like cyrillic or hindi) do not belong to \w.

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

To search special characters [ \ ^ $ . | ? * + ( ) literally, we need to prepend them with _________ (“escape them”).

A

\

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

what has flags?

regexp = /pattern/; 
regexp = /pattern/gmi;
A

regexp = /pattern/gmi;

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

let str = “HO-Ho-ho!”;

let result = str.match( /h(o)/ig );

alert( result ); //

A

HO, Ho, ho

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

let str = “HO-Ho-ho!”;

let result = str.match( /h(o)/i );

alert( result ); //

A

HO, O

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

let str = “HO-Ho-ho!”;

let result = str.match( /h(o)/ );

alert( result ); //

A

ho, o

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

let str = “HO-Ho-ho!”;

let result = str.match( /ho/i );

alert( result ); //

A

HO

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

alert(‘12-34-56’.split(‘-‘)) // array of [12, 34, 56]

alert(‘12-34-56’.split(_______)) // array of [12, 34, 56]

A

/-/

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

match – if there’s a ______ flag – returns all matches, without details parentheses,

A

g

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

let str = “+7(903)-123-45-67”;
let reg = /\d/g;
alerty(______) // 79035419441

A

str.match(reg).join(‘’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q
let str = "CSS4 is cool";
let reg = /CSS\\_\_\_\_\_\_

alert( str.match(reg) ); // CSS4

A

d/

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

\D

A

Non-digit: any character except \d, for instance a letter.

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

\S

A

Non-space: any character except \s, for instance a letter.

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

\W

A

Non-wordly character: anything but \w.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
\B
Non-boundary: a test reverse to \b.
26
alert( "Hello, Java!".match(/\bJava\b/) ); //
Java
27
__________is a special character class that matches “any character except a newline”.
The dot "."
28
Several characters or character classes _____________ mean to “search for any character among given”.
inside square brackets […]
29
_________ is a character in range from a to z, and _______is a digit from 0 to 5.
[a-z] | [0-5]
30
\d – is the same as _____
[0-9]
31
\w – is the same as _______
[a-zA-Z0-9_]
32
Besides normal ranges, there are “excluding” ranges that look like.
[^…] | CARROT
33
[^0-9] – any character except a digit, the same as _____
\D
34
[^\s] – any non-space character, same as _____________
\S
35
let str = "+7(903)-123-45-67"; alert( str.match(/\d+/g) ); //
7,903,123,45,67
36
let str = "+7(903)-123-45-67"; alert( str.match(/\d/g) ); //
7,9,0,3,1,2,3,4,5,6,7
37
alert( "I'm 12345 years old".match(/\d{5}/) ); //
"12345"
38
\d{5} denotes exactly 5 digits, the same as______
\d\d\d\d\d.
39
The simplest quantifier is a number __________ A quantifier is appended to a character (or a character class, or a [...] set etc) and specifies how many we need.
in curly braces: {n}.
40
Means “one or more”, the same as {1,}
+
41
Means “zero or one”, the same as {0,1}. In other words, it makes the symbol optional.
?
42
Means “zero or more”, the same as {0,}. That is, the character may repeat any times or be absent.
*
43
let str = "+7(903)-123-45-67"; alert(str.match(______) ); // 7,903,123,45,67
/\d+/g
44
the KEY: With this flag the search is case-insensitive: no difference between A and a.
i
45
the KEY: With this flag the search looks for all matches, without it – only the first one
g
46
the KEY: Multiline mode
m
47
to use a key you use use ____
/
48
let str = "+7(903)-123-45-67"; let reg = /\d/; alert(str.match(reg).join('')); RETURNS?
7 | the first digit
49
let str = "+7(903)-123-45-67"; let reg = /\D/; alert(str.match(reg).join('')); RETURNS?
+
50
let str = "+7(903)-123-45-67"; let reg = /\D/g; alert(str.match(reg).join('')); RETURNS?
+()---
51
WHY DOES THIS GIVE ERROR? let str = "+7( 903)-123-45-67"; let reg = /d/g; alert(str.match(reg).join(''));
need \ in front of d
52
``` let str = "JavaScript is a programming language"; let result = str.match( /JAVA(SCRIPT)/i ); ``` alert( result[0] ); //
JavaScript (the whole match)
53
``` let str = "JavaScript is a programming language"; let result = str.match( /JAVA(SCRIPT)/i ); ``` alert( result[1] ); //
script (the part of the match that corresponds to the parentheses)
54
``` let str = "JavaScript is a programming language"; let result = str.match( /JAVA(SCRIPT)/i ); ``` alert( result[result.index] ); //
0
55
``` let str = "JavaScript is a programming language"; let result = str.match( /JAVA(SCRIPT)/i ); ``` alert( result.input ); //
JavaScript is a programming language
56
let str = "Hey-hey-hey!"; alert( str.match(/Z/g).length ); //
Error: Cannot read property 'length' of null
57
_______ has no properties
NULL
58
let str = "Javascript or JavaScript? Should we uppercase 'S'?"; let result = str.matchAll( /java(script)/ig ); let [match1, match2] = result; alert( match1[0] ); //
Javascript (the whole match)
59
``` let str = "Javascript or JavaScript? Should we uppercase 'S'?"; let result = str.matchAll( /java(script)/ig ); let [match1, match2] = result; ``` alert( match2.index ); // alert( match2.input ); //
14 = str (the whole original string)
60
let str = "Javascript or JavaScript??"; let result = str.matchAll( /javascript/ig ); alert(result[0]); //
undefined (?! there must be a match) matchAll returns an iterable, not array
61
returns "undefined" how to fix ``` let str = "Javascript or JavaScript??"; let result = str.matchAll( /javascript/ig ); alert(result[0]); // undefined (?! there must be a match) ```
brackets let [result] = str.matchAll( /javascript/ig );`
62
alert('12-34-56'.replace("-", ":")) //
12:34-56
63
replace all - with : alert( '12-34-56'.replace( _______ ) ) // 12:34:56
/-/g, ":"
64
what to add so all - are replaced by : alert( '12-34-56'.replace( /-/, ":" ) ) // 12:34:56
g flag
65
The method _________ looks for a match and returns true/false whether it finds it.
regexp.test(str)
66
let str = "I love JavaScript"; // these two tests do the same alert( /love/i.test(str) ); // alert( str.search(/love/i) != -1 ); //
true | true
67
let str = "Bla-bla-bla"; alert( /love/i.test(str) ); //
false
68
let regexp = /love/gi; let str = "I love JavaScript"; ``` // start the search from position 10: ______________ alert( regexp.test(str) ); // false (no match) ```
regexp.lastIndex = 10
69
alert( "Hello, Java!".match(/\bJava\b/) ); //
Java
70
alert( "Hello, JavaScript!".match(/\bJava\b/) ); //
null
71
alert( "Hello, Java!".match(/\bHello\b/) ); //
Hello
72
alert( "Hello, Java!".match(/\bJava\b/) ); //
Java
73
alert( "Hello, Java!".match(/\bHell\b/) ); //
null (no match)
74
alert( "Hello, Java!".match(/\bJava!\b/) ); //
null (no match)
75
HOW TO RETURN / alert( "/".match(_____); // '/'
/\//
76
If we’re not using /.../, but create a regexp using \__________ then we don’t need to escape it:
new RegExp
77
If we are creating a regular expression with new RegExp, then we don’t have the _________
/
78
We also need to escape / if we’re inside /.../ but not inside ___________
new RegExp)
79
[a-zA-Z0-9_] is the same as the flay
w
80
let str = "+7(903)-123-45-67"; console.log( str.match(________ ); ["7", "903", "123", "45", "67"]
/\d+/g)
81
let str = "+777(903)-123-45-67"; console.log( str.match(/\d/) ); // returns?
7
82
let str = "+777(903)-123-45-67"; console.log( str.match(/\d+/) ); //
777
83
let str = "HO-Ho-ho!"; let result = str.match( /ho/ ); console.log( result ); //
ho
84
let str = "+7(903)-123-45-67"; alert( str.match(____________); // 7,9,0,3,1,2,3,4,5,6,7
/\d/g)
85
let str = "I love JavaScript"; rewrite this alert() alert( str.search(/love/i) != -1 ); //
/love/i.test(str)
86
alert( "Hello, Java Script!".match(/\bJava\b/) ); //
Java
87
alert( "Hello, Java!".match(/\bJava\b/) ); //
Java
88
_______ denotes exactly 5 digits, the same as \d\d\d\d\d\
\d{5}
89
let str = `1st place: Winnie 2nd place: Piglet 33rd place: Eeyore`; alert( str.match(/\w+$/gim) ); //
Winnie,Piglet,Eeyore
90
let str = `1st place: Winnie 2nd place: Piglet 33rd place: Eeyore`; alert( str.match(_________) ); // Winnie,Piglet,Eeyore
/\w+$/gim
91
let str = `1st place: Winnie 2nd place: Piglet 33rd place: Eeyore`; alert( str.match(__________ ); // 1
/^\d+/g)
92
let str = `1st place: Winnie 2nd place: Piglet 33rd place: Eeyore`; The regular expression _______ finds the last word in every line
\w+$
93
The first difference is that unlike anchors, the character _______ “consumes” the newline character and adds it to the result.
\n
94
let str = `1st place: Winnie 2nd place: Piglet 33rd place: Eeyore`; alert( str.match(/\w+/gim) );
1st place: Winnie, 2nd place: Piglet, 33rd place: Eeyore
95
var re = new RegExp('ab+c'); REWRITE . var re = ___________
/ab+c/;
96
``` var myRe = /d(b+)d/g; var myArray = myRe.exec('cdbbdbsbz'); console.log('The value of lastIndex is ' + myRe.lastIndex); ``` //
"The value of lastIndex is 5"
97
Matches beginning of input.
^
98
Matches end of input.
$
99
Matches the preceding item a, 0 or more times.
a*
100
Matches the preceding item a, 1 or more times.
a+
101
let str = "HO-Ho-ho!"; let result = str.match( ________ ); alert( result ); // HO, Ho, ho
/h(o)/ig
102
let str = "HO-Ho-ho!"; let result = str.match(________ ); alert( result ); // ho, o
/h(o)/
103
``` console.log(/abc/.test("abcde")); // → ```
true
104
``` console.log(/abc/.test("abxde")); // → ```
false
105
``` console.log(/[0123456789]/.test("in 1992")); // → ```
true
106
``` console.log(/[0-9]/.test("in 1992")); // → ```
true
107
console.log(_____________.test("in 1992")); // → true console.log(_____________.test("in 1992")); // → true
/[0123456789]/ /[0-9]/
108
``` console.log(/'\d'/.test("'123'")); // → ```
false
109
console.log(/'\d'/.test("'123'")); Make this true
addd + to \d
110
``` let neighbor = /neighbou?r/; console.log(neighbor.test("neighbour")); // → console.log(neighbor.test("neighbor")); // → ```
true true
111
let neighbor = /neighbou____r/;
?
112
console.log(/[0123456789]/.test("1e"));
true
113
let str = "+7(903)-123-45-67"; alert( str.match(/\d/g) ); // what to add so we DONT get single digit return
+ to \d
114
/[a-c]/.test('dc') // true or false
true
115
/[^A-Za-z0-9]/.test('@') // true or false
true
116
/^\d{3}$/.test('123') // /^\d{3}$/.test('12') // /^\d{3}$/.test('1234') //
true false false
117
/^\d{3,5}$/.test('123') // /^\d{3,5}$/.test('123456') //
true false
118
/^\d{3,}$/.test('12') // | /^\d{3,}$/.test('123') //
false | true
119
/^\d{3}\w?$/.test('123') // | /^\d{3}\w?$/.test('123ab') //
true | false
120
/^(\d{3})(\w+)$/.test('123') // | /^(\d{3})(\w+)$/.test('123s') //
false | true
121
/^(\d{3})(\w+)$/.test('123something') // | /^(\d{3})(\w+)$/.test('1234') //
true | true
122
/^\d{3,5}$/.test('1234') // | /^\d{3,5}$/.test('12345') //
true | true
123
/^(\d{2})+$/ /^(\d{2})+$/.test('12') //
true
124
'I saw a bear'.match(/\bbear/) //
Array ["bear"]
125
'I saw a beard'.match(/\bbear/)
//Array ["bear"]
126
'I saw a beard'.match(/\bbear\b/)
null
127
'cool_bear'.match(/\bbear\b/) //
null
128
The ____ character at the beginning of a pattern anchors it to the beginning of a string.
^
129
/hey|ho/.exec('hey') //
[ "hey" ]
130
let str1 = "Mary had a little lamb" alert( /_____Mary/.test(str1) ); // trie
^
131
INSERT WHAT REG. EXPRESSION ``` let str1 = "Mary had a little lamb"; alert( /lamb_______/.test(str1) ); // true ```
$
132
let str1 = "Mary had a little Lamb"; alert( /lamb$/i.test(str1) ); //
true
133
let str = `1st place: Winnie 2nd place: Piglet 33rd place: Eeyore`; alert( str.match(/\w/gim) );
all characters separated by ,
134
``` let str = "JavaScript is a programming language"; let result = str.match( /JAVASCRIPT/i ); ``` alert( result[1] ); //
undefined
135
write shorthand version let str = "+7(903)-123-45-67"; ----> let numbers = str.match(/\d{1,}/g); alert(numbers); // 7,903,123,45,67
let numbers = str.match(/\d+/g);
136
alert(/^(\d{3})(\w)$/.test('123something'));
FALSE
137
* OR + alert( "100 10 1".match(/\d0____/g) ); // 100, 10, 1
*
138
* OR + | alert( "100 10 1".match(/\d0____/g) ); // 100, 10
+