Check if an element is closed using a discrete tag with JavaScript

后端 未结 4 712
感动是毒
感动是毒 2021-01-05 04:47

I am getting the child nodes of en element and i want to check if the tags can actually contain text. For example:


,
4条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-05 05:25

    Could use RegEx:

    // these are samples, get elements text using element.innerHTML;
    var one = "Links";
    var two = "Lorem ipsum dolor";
    
    function check(str){
        return str.match(/<[a-zA-Z]+.*>(.|\n)*<\/[a-zA-Z]+>/) ? true : false;
    }
    
    console.log(check(one)); // true
    console.log(check(two)); // false
    

提交回复
热议问题