Skip to content

Commit 3b07d4e

Browse files
committed
Add more examples
1 parent cfeffcd commit 3b07d4e

7 files changed

Lines changed: 110 additions & 7 deletions

File tree

JavaScript/1-syntax.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use strict';
2+
3+
// Docs: https://developer.mozilla.org/en-US/docs/Web
4+
// /JavaScript/Reference/Global_Objects/RegExp
5+
6+
/*
7+
. - single char
8+
x? - optional or non-greedy
9+
x+ - 1 or more times
10+
x* - 0 or more times
11+
() - group $1...$9
12+
[] - chars
13+
\x - escape x
14+
^x - line begin or negated expr
15+
x$ - line end
16+
x|y - either x or y
17+
*/
18+
19+
const rx1 = /abc/;
20+
console.log('Do you know abc?'.match(rx1));
21+
22+
const rx2 = new RegExp('abc');
23+
console.log('Do you know abc?'.match(rx2));
24+
25+
const rx3 = /[a-z]+a[a-z]+/g;
26+
const st3 = 'A man can die but once';
27+
console.log(st3.match(rx3));
28+
29+
const rx4 = /\sg\w*/g;
30+
const st4 = 'Some are born great, ' +
31+
'some achieve greatness, ' +
32+
'and some have greatness thrust upon them.';
33+
console.log(st4.match(rx4));
34+
35+
const rx5 = /.u../g;
36+
const st5 = '— Such much? — For whom how';
37+
console.log(st5.match(rx5));
38+
39+
const rx6 = /\w{3,5}/g;
40+
const st6 = '— MGIMO finished? — Ask?!';
41+
console.log(st6.match(rx6));
42+
43+
const rx7 = /[^l] /g;
44+
const st7 = 'Nothing will come of nothing';
45+
console.log(st7.match(rx7));
46+
47+
const rx8 = /^\+?\d{12}$/;
48+
const st8 = '+380661234567';
49+
console.log(st8.match(rx8));
50+
51+
const rx9 = /[0-9]+ (hours|days)/g;
52+
const st9 = '5 days';
53+
console.log(st9.match(rx9));

JavaScript/2-regexp.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
const rx = /[abc]/gi;
4+
5+
console.dir({
6+
rx,
7+
flags: rx.flags,
8+
global: rx.global,
9+
ignoreCase: rx.ignoreCase,
10+
multiline: rx.multiline,
11+
source: rx.source,
12+
sticky: rx.sticky,
13+
unicode: rx.unicode,
14+
lastIndex: rx.lastIndex,
15+
});
16+
17+
console.dir({
18+
xyz: rx.test('xyz'),
19+
abc: rx.test('abc')
20+
});
21+
22+
const s = 'abcdefgabc';
23+
24+
let res;
25+
do {
26+
res = rx.exec(s);
27+
console.log({ lastIndex: rx.lastIndex, res });
28+
} while (res);

JavaScript/3-match.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict';
22

3-
const s = 'Hello user1@domain.com and user2@domain.com';
3+
const s = 'Hello <User1@domain.com> and user2@domain.com';
44

5-
const rx = /\S+@[\S.]+/g;
5+
const rx = /[a-z0-9\.]+@[a-z0-9\.]+/gi;
66

77
const m = s.match(rx);
88

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
'use strict';
22

3-
const s = 'Hello World, here we are!';
4-
5-
const words = (s
3+
const words = s => [...new Set(s
64
.toLowerCase()
75
.replace(/\W+/g, ' ')
86
.trim()
97
.split(/\s/)
10-
);
8+
)];
9+
10+
const s = 'Hello World, here we are!';
1111

12-
console.dir(words);
12+
console.dir(words(s));
13+
console.dir(words(s + s));

JavaScript/6-replace-fn.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
3+
const rx = /abc/gi;
4+
const s = 'abcdefgAbC';
5+
const res = s.replace(rx, (sub, pos, str) => {
6+
console.dir({ sub, pos, str });
7+
return sub.toUpperCase();
8+
});
9+
10+
console.log(res);

JavaScript/7-search.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
const rx1 = /def/g;
4+
const s1 = 'abcdefgabc';
5+
const res1 = s1.search(rx1);
6+
console.log(res1);
7+
8+
const rx2 = /cba/g;
9+
const s2 = 'abcdefgabc';
10+
const res2 = s2.search(rx2);
11+
console.log(res2);

0 commit comments

Comments
 (0)