-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbetweenMarkers.js
More file actions
33 lines (23 loc) · 1.15 KB
/
Copy pathbetweenMarkers.js
File metadata and controls
33 lines (23 loc) · 1.15 KB
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
// ??? ðàáî÷èé ëè?
function betweenMarkers(line, left, right) {
let regex = `(\\${left})(.+)(\\${right})`;
return line.match(regex)? line.match(regex)[2] : null;
}
console.log(betweenMarkers('What is ><', '>', '<'));
// ýòîò òî÷íî ðàáî÷èé
function betweenMarkers(text: string, begin: string, end: string): string {
let bI = (text.indexOf(begin)!= -1) ? text.indexOf(begin)+ begin.length : 0;
let eI = (text.indexOf(end)!= -1) ? text.indexOf(end) : text.length;
let res = text.slice(bI, eI);
return res;
}
console.log('Example:')
console.log(betweenMarkers('What is >apple<', '>', '<'), 'apple')
assert.equal(betweenMarkers('What is >apple<', '>', '<'), 'apple')
assert.equal(betweenMarkers("<head><title>My new site</title></head>",
"<title>", "</title>"), 'My new site')
assert.equal(betweenMarkers('No[/b] hi', '[b]', '[/b]'), 'No')
assert.equal(betweenMarkers('No [b]hi', '[b]', '[/b]'), 'hi')
assert.equal(betweenMarkers('No hi', '[b]', '[/b]'), 'No hi')
assert.equal(betweenMarkers('No <hi>', '>', '<'), '')
console.log("Coding complete? Click 'Check' to review your tests and earn cool rewards!");