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