JavaScript detects if the string contains the html tag implementation method

  • 2020-06-22 23:55:58
  • OfStack

Function code


/**
 * Whether the string contains html Detection of labels
 * @param htmlStr
 */
function checkHtml(htmlStr) {
    var  reg = /<[^>]+>/g;
    return reg.test(htmlStr);
}

demo
script:

/**
 * Whether the string contains html Detection of labels
 * @param htmlStr
 */
function checkHtml(htmlStr) {
    var  reg = /<[^>]+>/g;
    return reg.test(htmlStr);
}
window.onload=function(){
    var html=document.querySelector('#box').innerHTML;
    alert(checkHtml(html))
}

html

 <div id="box">
        sdfsdf
        <div></div>
        <span>sdfsdf</span>
    </div>


Related articles: