-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregexp.js
106 lines (82 loc) · 2.52 KB
/
regexp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
const devision = title => {console.log(`\n------------${title}-------------`)}
let string = 'all your base are belong to us'
let regex = /base/
let faildString = ' all you are belong to us'
// see if a smaller string exists in a bigger string
// using `test`
const isExisting = str => regex.test(str)
console.log(string, regex, isExisting(string))
console.log(faildString, regex, isExisting(faildString))
devision('^$ sign')
string = 'baseaaa'
faildString = '-base-'
regex = /^base/
console.log(string, regex, isExisting(string))
console.log(faildString, regex, isExisting(faildString))
regex = /base$/
string = 'aaabase'
console.log(string, regex, isExisting(string))
console.log(faildString, regex, isExisting(faildString))
string = 'base'
regex = /^base$/
console.log(string, regex, isExisting(string))
console.log(faildString, regex, isExisting(faildString))
devision('[] and + * ?')
string = 'my name is something and my age is 24'
faildString = 'my name is Smething and my age is 24'
// will find the match in wich the 'name is [a-z]+' is the substring of all the letter form
// a -> z and the + is that can be one or more matchs of [a-z]
regex = /name is [a-z]+/
// fuctions that return the match
// the exec return an array of information from the regex expression
const match = str => {
let arr = regex.exec(str)
arr == null ? console.log(arr, 'erro!!') : console.log('result ->', arr[0])
}
console.log(string, regex, isExisting(string))
match(string)
console.log(faildString, regex, isExisting(faildString))
match(faildString)
faildString = 'my name is somethin and my age is 24'
faildString2 = 'my name is somethi1 and my age is 24'
regex = /my name is something?/
console.log(regex)
match(string)
match(faildString)
match(faildString2)
devision('{size}')
regex = /name is [a-z]{1,3}/
console.log(string, regex, isExisting(string))
match(string)
devision('grouping ()')
string = 'name is something'
regex = /name is ([a-z]+)/
const matchName = regex.exec(string)
// the array obtain from the regex expression
// contains in the position 1 the grouped expression
console.log(matchName)
const name = matchName[1]
console.log(name)
devision('small and more complex example')
string = `
HELLO, LOOK_AT mp3 YOU
qw12_3file.mp3
fe.mp4
fle.mov
fie.pdf
le.mp3
le.pdf
fle.mp3
fe.pdf
e.pdf
fi.mp3`
//regex = /(\w+)\.mp3/
regex = /\w+\.mp3/g
let match1 = string.match(regex)
console.log('----------', match1, '----------')
while (match1) {
const files = match1[1]
console.log(match1)
console.log(files)
match1 = regex.exec(string)
}