Regular Expressions Flashcards
(138 cards)
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.