简体   繁体   中英

How to validate well-formed XML (greater/less than) tags using specific keywords in JavaScript?

I am building an application that allows users to build web pages by entering XML style tags in an HTML textarea form.

The tags array contains possible element names eg 'text,'image','heading' that are formatted as <text>The text within</text> in the textarea.

var tags = ["text", "image", "heading"];
var text = document.getElementById('textArea').value;

How can I validate that all opening tags are accompanied by closing tags containing the same element name and that tags are only valid if they contain a keyword from the valid keyword array?

I have been trying to search, and try this :

 function wellFormat(html) { var doc = document.createElement('div'); doc.innerHTML = html; return ( doc.innerHTML === html ); } console.log(wellFormat('<text>The text within</text>')) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM