Verify whether a string is a valid HTML tag name

后端 未结 3 947
小蘑菇
小蘑菇 2021-01-21 21:22

How can I find out if a string is a valid HTML tag?

For example: a, or h1, div, or span, ... are a valid HTML tagname. But ar, or abc, or div2, ... are invaild.

3条回答
  •  温柔的废话
    2021-01-21 22:02

    You will have to get and use your own array of HTML tags to check this - I would personally go with W3C spec instead of the w3schools one.

    Then, you want to use indexOf on the array and check if it's not -1, something like this:

    var isValid = function (tagCandidate) { return (validHtmlTags.indexOf(tagCandidate) > -1); };

提交回复
热议问题