-
Notifications
You must be signed in to change notification settings - Fork 2
/
html_elements_stack.js
39 lines (35 loc) · 1.05 KB
/
html_elements_stack.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
function HTMLElements(str) {
// const a = str.replace(/\>[A-Za-z\s]*\</g, "");
// console.log(a);
// *\>[^)]*\< *
// const a = str.replace(/ *\>[^<]*\) */g, "");
// str.replace(/\>[a-z]*\</g, "");
const stack = [];
let elements = str.split("<").join('').split(">");
// removing text eg "text test test/b" -> "/b"
for(let i=0; i< elements.length; i++){
if(elements[i].indexOf("/") > -1){
elements[i] = elements[i].substring(elements[i].indexOf("/"));
}
}
console.log(elements);
let i = 0;
while(elements[i]){
if(elements[i][0] == '/'){
// if peep of stack is same as this element with /, then pop it out else return it
const peep = stack[stack.length - 1];
if(`/${peep}` == elements[i]){
stack.pop();
} else {
return peep;
}
} else {
stack.push(elements[i]);
}
i++;
}
return true;
}
// keep this function call here
// console.log(HTMLElements("<div><div><b></b></div></p>"));
console.log(HTMLElements("<div>abc</div><p><em><i>test test test</b></em></p>"));