Regular Expressions Flashcards
i
With this flag the search is case-insensitive: no difference between A and a.
g
With this flag the search looks for all matches, without it – only the first one
m
Multiline mode
s
“Dotall” mode, allows . to match newlines
u
Enables full unicode support.
y
Sticky mode
let str = “I love JavaScript!”;
alert( str.search(/LOVE/i) ); //
alert( str.search(/LOVE/) ); //
2 (found lowercased)
-1 (nothing found without ‘i’ flag)
let str = “A drop of ink may make a million think”;
alert( str.search( /a/ ) );
15
first “a” is 15 and it’s case sensative
\d
A digit: a character from 0 to 9.
\s
A space symbol: that includes spaces, tabs, newlines.`
\w
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.
To search special characters [ \ ^ $ . | ? * + ( ) literally, we need to prepend them with _________ (“escape them”).
\
what has flags?
regexp = /pattern/; regexp = /pattern/gmi;
regexp = /pattern/gmi;
let str = “HO-Ho-ho!”;
let result = str.match( /h(o)/ig );
alert( result ); //
HO, Ho, ho
let str = “HO-Ho-ho!”;
let result = str.match( /h(o)/i );
alert( result ); //
HO, O
let str = “HO-Ho-ho!”;
let result = str.match( /h(o)/ );
alert( result ); //
ho, o
let str = “HO-Ho-ho!”;
let result = str.match( /ho/i );
alert( result ); //
HO
alert(‘12-34-56’.split(‘-‘)) // array of [12, 34, 56]
alert(‘12-34-56’.split(_______)) // array of [12, 34, 56]
/-/
match – if there’s a ______ flag – returns all matches, without details parentheses,
g
let str = “+7(903)-123-45-67”;
let reg = /\d/g;
alerty(______) // 79035419441
str.match(reg).join(‘’)
let str = "CSS4 is cool"; let reg = /CSS\\_\_\_\_\_\_
alert( str.match(reg) ); // CSS4
d/
\D
Non-digit: any character except \d, for instance a letter.
\S
Non-space: any character except \s, for instance a letter.
\W
Non-wordly character: anything but \w.
\B
Non-boundary: a test reverse to \b.
alert( “Hello, Java!”.match(/\bJava\b/) ); //
Java
__________is a special character class that matches “any character except a newline”.
The dot “.”
Several characters or character classes _____________ mean to “search for any character among given”.
inside square brackets […]
_________ is a character in range from a to z, and _______is a digit from 0 to 5.
[a-z]
[0-5]
\d – is the same as _____
[0-9]
\w – is the same as _______
[a-zA-Z0-9_]
Besides normal ranges, there are “excluding” ranges that look like.
[^…]
CARROT
[^0-9] – any character except a digit, the same as _____
\D
[^\s] – any non-space character, same as _____________
\S
let str = “+7(903)-123-45-67”;
alert( str.match(/\d+/g) ); //
7,903,123,45,67
let str = “+7(903)-123-45-67”;
alert( str.match(/\d/g) ); //
7,9,0,3,1,2,3,4,5,6,7
alert( “I’m 12345 years old”.match(/\d{5}/) ); //
“12345”
\d{5} denotes exactly 5 digits, the same as______
\d\d\d\d\d.
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}.
Means “one or more”, the same as {1,}
+
Means “zero or one”, the same as {0,1}. In other words, it makes the symbol optional.
?
Means “zero or more”, the same as {0,}. That is, the character may repeat any times or be absent.
*
let str = “+7(903)-123-45-67”;
alert(str.match(______) ); // 7,903,123,45,67
/\d+/g
the KEY:
With this flag the search is case-insensitive: no difference between A and a.
i
the KEY:
With this flag the search looks for all matches, without it – only the first one
g
the KEY:
Multiline mode
m
to use a key you use use ____
/
let str = “+7(903)-123-45-67”;
let reg = /\d/;
alert(str.match(reg).join(‘’));
RETURNS?
7
the first digit
let str = “+7(903)-123-45-67”;
let reg = /\D/;
alert(str.match(reg).join(‘’));
RETURNS?
+
let str = “+7(903)-123-45-67”;
let reg = /\D/g;
alert(str.match(reg).join(‘’));
RETURNS?
+()—
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
let str = "JavaScript is a programming language"; let result = str.match( /JAVA(SCRIPT)/i );
alert( result[0] ); //
JavaScript (the whole match)
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)
let str = "JavaScript is a programming language"; let result = str.match( /JAVA(SCRIPT)/i );
alert( result[result.index] ); //
0
let str = "JavaScript is a programming language"; let result = str.match( /JAVA(SCRIPT)/i );
alert( result.input ); //
JavaScript is a programming language
let str = “Hey-hey-hey!”;
alert( str.match(/Z/g).length ); //
Error: Cannot read property ‘length’ of null
_______ has no properties
NULL
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)
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)
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
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 );`
alert(‘12-34-56’.replace(“-“, “:”)) //
12:34-56
replace all - with :
alert( ‘12-34-56’.replace( _______ ) ) // 12:34:56
/-/g, “:”
what to add so all - are replaced by :
alert( ‘12-34-56’.replace( /-/, “:” ) ) // 12:34:56
g flag
The method _________ looks for a match and returns true/false whether it finds it.
regexp.test(str)
let str = “I love JavaScript”;
// these two tests do the same
alert( /love/i.test(str) ); //
alert( str.search(/love/i) != -1 ); //
true
true
let str = “Bla-bla-bla”;
alert( /love/i.test(str) ); //
false
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
alert( “Hello, Java!”.match(/\bJava\b/) ); //
Java
alert( “Hello, JavaScript!”.match(/\bJava\b/) ); //
null
alert( “Hello, Java!”.match(/\bHello\b/) ); //
Hello
alert( “Hello, Java!”.match(/\bJava\b/) ); //
Java
alert( “Hello, Java!”.match(/\bHell\b/) ); //
null (no match)
alert( “Hello, Java!”.match(/\bJava!\b/) ); //
null (no match)
HOW TO RETURN /
alert( “/”.match(_____); // ‘/’
/\//
If we’re not using /…/, but create a regexp using \__________ then we don’t need to escape it:
new RegExp
If we are creating a regular expression with new RegExp, then we don’t have the _________
/
We also need to escape / if we’re inside /…/ but not inside ___________
new RegExp)
[a-zA-Z0-9_] is the same as the flay
w
let str = “+7(903)-123-45-67”;
console.log( str.match(________ );
[“7”, “903”, “123”, “45”, “67”]
/\d+/g)
let str = “+777(903)-123-45-67”;
console.log( str.match(/\d/) ); //
returns?
7
let str = “+777(903)-123-45-67”;
console.log( str.match(/\d+/) ); //
777
let str = “HO-Ho-ho!”;
let result = str.match( /ho/ );
console.log( result ); //
ho
let str = “+7(903)-123-45-67”;
alert( str.match(____________); //
7,9,0,3,1,2,3,4,5,6,7
/\d/g)
let str = “I love JavaScript”;
rewrite this alert()
alert( str.search(/love/i) != -1 ); //
/love/i.test(str)
alert( “Hello, Java Script!”.match(/\bJava\b/) ); //
Java
alert( “Hello, Java!”.match(/\bJava\b/) ); //
Java
_______ denotes exactly 5 digits, the same as \d\d\d\d\d\
\d{5}
let str = 1st place: Winnie
2nd place: Piglet
33rd place: Eeyore
;
alert( str.match(/\w+$/gim) ); //
Winnie,Piglet,Eeyore
let str = 1st place: Winnie
2nd place: Piglet
33rd place: Eeyore
;
alert( str.match(_________) ); // Winnie,Piglet,Eeyore
/\w+$/gim
let str = 1st place: Winnie
2nd place: Piglet
33rd place: Eeyore
;
alert( str.match(__________ ); // 1
/^\d+/g)
let str = 1st place: Winnie
2nd place: Piglet
33rd place: Eeyore
;
The regular expression _______ finds the last word in every line
\w+$
The first difference is that unlike anchors, the character _______ “consumes” the newline character and adds it to the result.
\n
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
var re = new RegExp(‘ab+c’);
REWRITE . var re = ___________
/ab+c/;
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”
Matches beginning of input.
Matches end of input.
$
Matches the preceding item a, 0 or more times.
a*
Matches the preceding item a, 1 or more times.
a+
let str = “HO-Ho-ho!”;
let result = str.match( ________ );
alert( result ); // HO, Ho, ho
/h(o)/ig
let str = “HO-Ho-ho!”;
let result = str.match(________ );
alert( result ); // ho, o
/h(o)/
console.log(/abc/.test("abcde")); // →
true
console.log(/abc/.test("abxde")); // →
false
console.log(/[0123456789]/.test("in 1992")); // →
true
console.log(/[0-9]/.test("in 1992")); // →
true
console.log(_____________.test(“in 1992”));
// → true
console.log(_____________.test(“in 1992”));
// → true
/[0123456789]/
/[0-9]/
console.log(/'\d'/.test("'123'")); // →
false
console.log(/’\d’/.test(“‘123’”));
Make this true
addd + to \d
let neighbor = /neighbou?r/; console.log(neighbor.test("neighbour")); // → console.log(neighbor.test("neighbor")); // →
true
true
let neighbor = /neighbou____r/;
?
console.log(/[0123456789]/.test(“1e”));
true
let str = “+7(903)-123-45-67”;
alert( str.match(/\d/g) ); //
what to add so we DONT get single digit return
+ to \d
/[a-c]/.test(‘dc’) // true or false
true
/[^A-Za-z0-9]/.test(‘@’) // true or false
true
/^\d{3}$/.test(‘123’) //
/^\d{3}$/.test(‘12’) //
/^\d{3}$/.test(‘1234’) //
true
false
false
/^\d{3,5}$/.test(‘123’) //
/^\d{3,5}$/.test(‘123456’) //
true
false
/^\d{3,}$/.test(‘12’) //
/^\d{3,}$/.test(‘123’) //
false
true
/^\d{3}\w?$/.test(‘123’) //
/^\d{3}\w?$/.test(‘123ab’) //
true
false
/^(\d{3})(\w+)$/.test(‘123’) //
/^(\d{3})(\w+)$/.test(‘123s’) //
false
true
/^(\d{3})(\w+)$/.test(‘123something’) //
/^(\d{3})(\w+)$/.test(‘1234’) //
true
true
/^\d{3,5}$/.test(‘1234’) //
/^\d{3,5}$/.test(‘12345’) //
true
true
/^(\d{2})+$/
/^(\d{2})+$/.test(‘12’) //
true
‘I saw a bear’.match(/\bbear/) //
Array [“bear”]
‘I saw a beard’.match(/\bbear/)
//Array [“bear”]
‘I saw a beard’.match(/\bbear\b/)
null
‘cool_bear’.match(/\bbear\b/) //
null
The ____ character at the beginning of a pattern anchors it to the beginning of a string.
/hey|ho/.exec(‘hey’) //
[ “hey” ]
let str1 = “Mary had a little lamb”
alert( /_____Mary/.test(str1) ); // trie
INSERT WHAT REG. EXPRESSION
let str1 = "Mary had a little lamb"; alert( /lamb\_\_\_\_\_\_\_/.test(str1) ); // true
$
let str1 = “Mary had a little Lamb”;
alert( /lamb$/i.test(str1) ); //
true
let str = 1st place: Winnie
2nd place: Piglet
33rd place: Eeyore
;
alert( str.match(/\w/gim) );
all characters separated by ,
let str = "JavaScript is a programming language"; let result = str.match( /JAVASCRIPT/i );
alert( result[1] ); //
undefined
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);
alert(/^(\d{3})(\w)$/.test(‘123something’));
FALSE
- OR +
alert( “100 10 1”.match(/\d0____/g) ); // 100, 10, 1
*
- OR +
alert( “100 10 1”.match(/\d0____/g) ); // 100, 10
+