I need to check if html is valid. Basically I need to check in string whether they closed or not. But I don't even know where to start.
Rules:
- It should return true if the string is valid, and false if it's invalid.
- All input strings will be nonempty, and will only consist of these html tags: .
Expected result
"<div></div>" => True
"<div><span></span></div>" => True
"<div></span>" => False
"<div>" => False
"<div><div><span></span></div></h1>" => False
So far I only have bare-bones. My Thoughts:
- I tried to split all tag elements into different array. Using this This answer. But that answer didn't worked for me, and I can't select empty space because there is isn't any.
After splitting I would probably use for loops with if statement to compare this. Because it's only 3 tags I don't need anything fancy.
const isHTMLValid = (html) => { html = html.replace(/>/gi, ",>"); html = html.split(','); console.log(html); } isHTMLValid("<div></div>"); isHTMLValid("<div><span></span></div>"); isHTMLValid("<div></span>"); isHTMLValid("<div>"); isHTMLValid("<div><div><span></span></div></h1>");
` - this is *valid* HTML but there is no nesting involved. And the tag `
` is equally valid. – VLAZ Mar 05 '19 at 13:22