Regular Expressions Flashcards

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
Q

\B

A

Non-boundary: a test reverse to \b.

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

alert( “Hello, Java!”.match(/\bJava\b/) ); //

A

Java

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

__________is a special character class that matches “any character except a newline”.

A

The dot “.”

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

Several characters or character classes _____________ mean to “search for any character among given”.

A

inside square brackets […]

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

_________ is a character in range from a to z, and _______is a digit from 0 to 5.

A

[a-z]

[0-5]

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

\d – is the same as _____

A

[0-9]

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

\w – is the same as _______

A

[a-zA-Z0-9_]

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

Besides normal ranges, there are “excluding” ranges that look like.

A

[^…]

CARROT

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

[^0-9] – any character except a digit, the same as _____

A

\D

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

[^\s] – any non-space character, same as _____________

A

\S

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

let str = “+7(903)-123-45-67”;

alert( str.match(/\d+/g) ); //

A

7,903,123,45,67

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

let str = “+7(903)-123-45-67”;

alert( str.match(/\d/g) ); //

A

7,9,0,3,1,2,3,4,5,6,7

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

alert( “I’m 12345 years old”.match(/\d{5}/) ); //

A

“12345”

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

\d{5} denotes exactly 5 digits, the same as______

A

\d\d\d\d\d.

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

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.

A

in curly braces: {n}.

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

Means “one or more”, the same as {1,}

A

+

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

Means “zero or one”, the same as {0,1}. In other words, it makes the symbol optional.

A

?

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

Means “zero or more”, the same as {0,}. That is, the character may repeat any times or be absent.

A

*

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

let str = “+7(903)-123-45-67”;

alert(str.match(______) ); // 7,903,123,45,67

A

/\d+/g

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

the KEY:

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

A

i

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

the KEY:

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

A

g

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

the KEY:

Multiline mode

A

m

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

to use a key you use use ____

A

/

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

let str = “+7(903)-123-45-67”;
let reg = /\d/;
alert(str.match(reg).join(‘’));

RETURNS?

A

7

the first digit

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

let str = “+7(903)-123-45-67”;
let reg = /\D/;
alert(str.match(reg).join(‘’));

RETURNS?

A

+

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

let str = “+7(903)-123-45-67”;
let reg = /\D/g;
alert(str.match(reg).join(‘’));

RETURNS?

A

+()—

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

WHY DOES THIS GIVE ERROR?

let str = “+7( 903)-123-45-67”;
let reg = /d/g;
alert(str.match(reg).join(‘’));

A

need \ in front of d

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
52
Q
let str = "JavaScript is a programming language";
let result = str.match( /JAVA(SCRIPT)/i );

alert( result[0] ); //

A

JavaScript (the whole match)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
53
Q
let str = "JavaScript is a programming language";
let result = str.match( /JAVA(SCRIPT)/i );

alert( result[1] ); //

A

script (the part of the match that corresponds to the parentheses)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
54
Q
let str = "JavaScript is a programming language";
let result = str.match( /JAVA(SCRIPT)/i );

alert( result[result.index] ); //

A

0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
55
Q
let str = "JavaScript is a programming language";
let result = str.match( /JAVA(SCRIPT)/i );

alert( result.input ); //

A

JavaScript is a programming language

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

let str = “Hey-hey-hey!”;

alert( str.match(/Z/g).length ); //

A

Error: Cannot read property ‘length’ of null

57
Q

_______ has no properties

A

NULL

58
Q

let str = “Javascript or JavaScript? Should we uppercase ‘S’?”;

let result = str.matchAll( /java(script)/ig );

let [match1, match2] = result;

alert( match1[0] ); //

A

Javascript (the whole match)

59
Q
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 ); //

A

14

= str (the whole original string)

60
Q

let str = “Javascript or JavaScript??”;

let result = str.matchAll( /javascript/ig );

alert(result[0]); //

A

undefined (?! there must be a match)

matchAll returns an iterable, not array

61
Q

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)
A

brackets let [result] = str.matchAll( /javascript/ig );`

62
Q

alert(‘12-34-56’.replace(“-“, “:”)) //

A

12:34-56

63
Q

replace all - with :

alert( ‘12-34-56’.replace( _______ ) ) // 12:34:56

A

/-/g, “:”

64
Q

what to add so all - are replaced by :

alert( ‘12-34-56’.replace( /-/, “:” ) ) // 12:34:56

A

g flag

65
Q

The method _________ looks for a match and returns true/false whether it finds it.

A

regexp.test(str)

66
Q

let str = “I love JavaScript”;

// these two tests do the same
alert( /love/i.test(str) ); //
alert( str.search(/love/i) != -1 ); //

A

true

true

67
Q

let str = “Bla-bla-bla”;

alert( /love/i.test(str) ); //

A

false

68
Q

let regexp = /love/gi;

let str = “I love JavaScript”;

// start the search from position 10:
\_\_\_\_\_\_\_\_\_\_\_\_\_\_
alert( regexp.test(str) ); // false (no match)
A

regexp.lastIndex = 10

69
Q

alert( “Hello, Java!”.match(/\bJava\b/) ); //

A

Java

70
Q

alert( “Hello, JavaScript!”.match(/\bJava\b/) ); //

A

null

71
Q

alert( “Hello, Java!”.match(/\bHello\b/) ); //

A

Hello

72
Q

alert( “Hello, Java!”.match(/\bJava\b/) ); //

A

Java

73
Q

alert( “Hello, Java!”.match(/\bHell\b/) ); //

A

null (no match)

74
Q

alert( “Hello, Java!”.match(/\bJava!\b/) ); //

A

null (no match)

75
Q

HOW TO RETURN /

alert( “/”.match(_____); // ‘/’

A

/\//

76
Q

If we’re not using /…/, but create a regexp using \__________ then we don’t need to escape it:

A

new RegExp

77
Q

If we are creating a regular expression with new RegExp, then we don’t have the _________

A

/

78
Q

We also need to escape / if we’re inside /…/ but not inside ___________

A

new RegExp)

79
Q

[a-zA-Z0-9_] is the same as the flay

A

w

80
Q

let str = “+7(903)-123-45-67”;

console.log( str.match(________ );
[“7”, “903”, “123”, “45”, “67”]

A

/\d+/g)

81
Q

let str = “+777(903)-123-45-67”;

console.log( str.match(/\d/) ); //
returns?

A

7

82
Q

let str = “+777(903)-123-45-67”;

console.log( str.match(/\d+/) ); //

A

777

83
Q

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

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

console.log( result ); //

A

ho

84
Q

let str = “+7(903)-123-45-67”;

alert( str.match(____________); //
7,9,0,3,1,2,3,4,5,6,7

A

/\d/g)

85
Q

let str = “I love JavaScript”;

rewrite this alert()
alert( str.search(/love/i) != -1 ); //

A

/love/i.test(str)

86
Q

alert( “Hello, Java Script!”.match(/\bJava\b/) ); //

A

Java

87
Q

alert( “Hello, Java!”.match(/\bJava\b/) ); //

A

Java

88
Q

_______ denotes exactly 5 digits, the same as \d\d\d\d\d\

A

\d{5}

89
Q

let str = 1st place: Winnie 2nd place: Piglet 33rd place: Eeyore;

alert( str.match(/\w+$/gim) ); //

A

Winnie,Piglet,Eeyore

90
Q

let str = 1st place: Winnie 2nd place: Piglet 33rd place: Eeyore;

alert( str.match(_________) ); // Winnie,Piglet,Eeyore

A

/\w+$/gim

91
Q

let str = 1st place: Winnie 2nd place: Piglet 33rd place: Eeyore;

alert( str.match(__________ ); // 1

A

/^\d+/g)

92
Q

let str = 1st place: Winnie 2nd place: Piglet 33rd place: Eeyore;

The regular expression _______ finds the last word in every line

A

\w+$

93
Q

The first difference is that unlike anchors, the character _______ “consumes” the newline character and adds it to the result.

A

\n

94
Q

let str = 1st place: Winnie 2nd place: Piglet 33rd place: Eeyore;

alert( str.match(/\w+/gim) );

A

1st place: Winnie, 2nd place: Piglet, 33rd place: Eeyore

95
Q

var re = new RegExp(‘ab+c’);

REWRITE . var re = ___________

A

/ab+c/;

96
Q
var myRe = /d(b+)d/g;
var myArray = myRe.exec('cdbbdbsbz');
console.log('The value of lastIndex is ' + myRe.lastIndex);

//

A

“The value of lastIndex is 5”

97
Q

Matches beginning of input.

A
98
Q

Matches end of input.

A

$

99
Q

Matches the preceding item a, 0 or more times.

A

a*

100
Q

Matches the preceding item a, 1 or more times.

A

a+

101
Q

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

let result = str.match( ________ );

alert( result ); // HO, Ho, ho

A

/h(o)/ig

102
Q

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

let result = str.match(________ );

alert( result ); // ho, o

A

/h(o)/

103
Q
console.log(/abc/.test("abcde"));
// →
A

true

104
Q
console.log(/abc/.test("abxde"));
// →
A

false

105
Q
console.log(/[0123456789]/.test("in 1992"));
// →
A

true

106
Q
console.log(/[0-9]/.test("in 1992"));
// →
A

true

107
Q

console.log(_____________.test(“in 1992”));
// → true
console.log(_____________.test(“in 1992”));
// → true

A

/[0123456789]/

/[0-9]/

108
Q
console.log(/'\d'/.test("'123'"));
// →
A

false

109
Q

console.log(/’\d’/.test(“‘123’”));

Make this true

A

addd + to \d

110
Q
let neighbor = /neighbou?r/;
console.log(neighbor.test("neighbour"));
// →
console.log(neighbor.test("neighbor"));
// →
A

true

true

111
Q

let neighbor = /neighbou____r/;

A

?

112
Q

console.log(/[0123456789]/.test(“1e”));

A

true

113
Q

let str = “+7(903)-123-45-67”;

alert( str.match(/\d/g) ); //

what to add so we DONT get single digit return

A

+ to \d

114
Q

/[a-c]/.test(‘dc’) // true or false

A

true

115
Q

/[^A-Za-z0-9]/.test(‘@’) // true or false

A

true

116
Q

/^\d{3}$/.test(‘123’) //
/^\d{3}$/.test(‘12’) //
/^\d{3}$/.test(‘1234’) //

A

true
false
false

117
Q

/^\d{3,5}$/.test(‘123’) //

/^\d{3,5}$/.test(‘123456’) //

A

true

false

118
Q

/^\d{3,}$/.test(‘12’) //

/^\d{3,}$/.test(‘123’) //

A

false

true

119
Q

/^\d{3}\w?$/.test(‘123’) //

/^\d{3}\w?$/.test(‘123ab’) //

A

true

false

120
Q

/^(\d{3})(\w+)$/.test(‘123’) //

/^(\d{3})(\w+)$/.test(‘123s’) //

A

false

true

121
Q

/^(\d{3})(\w+)$/.test(‘123something’) //

/^(\d{3})(\w+)$/.test(‘1234’) //

A

true

true

122
Q

/^\d{3,5}$/.test(‘1234’) //

/^\d{3,5}$/.test(‘12345’) //

A

true

true

123
Q

/^(\d{2})+$/

/^(\d{2})+$/.test(‘12’) //

A

true

124
Q

‘I saw a bear’.match(/\bbear/) //

A

Array [“bear”]

125
Q

‘I saw a beard’.match(/\bbear/)

A

//Array [“bear”]

126
Q

‘I saw a beard’.match(/\bbear\b/)

A

null

127
Q

‘cool_bear’.match(/\bbear\b/) //

A

null

128
Q

The ____ character at the beginning of a pattern anchors it to the beginning of a string.

A
129
Q

/hey|ho/.exec(‘hey’) //

A

[ “hey” ]

130
Q

let str1 = “Mary had a little lamb”

alert( /_____Mary/.test(str1) ); // trie

A
131
Q

INSERT WHAT REG. EXPRESSION

let str1 = "Mary had a little lamb";
alert( /lamb\_\_\_\_\_\_\_/.test(str1) ); // true
A

$

132
Q

let str1 = “Mary had a little Lamb”;

alert( /lamb$/i.test(str1) ); //

A

true

133
Q

let str = 1st place: Winnie 2nd place: Piglet 33rd place: Eeyore;

alert( str.match(/\w/gim) );

A

all characters separated by ,

134
Q
let str = "JavaScript is a programming language";
let result = str.match( /JAVASCRIPT/i ); 

alert( result[1] ); //

A

undefined

135
Q

write shorthand version

let str = “+7(903)-123-45-67”;
—-> let numbers = str.match(/\d{1,}/g);
alert(numbers); // 7,903,123,45,67

A

let numbers = str.match(/\d+/g);

136
Q

alert(/^(\d{3})(\w)$/.test(‘123something’));

A

FALSE

137
Q
  • OR +

alert( “100 10 1”.match(/\d0____/g) ); // 100, 10, 1

A

*

138
Q
  • OR +

alert( “100 10 1”.match(/\d0____/g) ); // 100, 10

A

+