JS清除HTML标签转换成<br/;

Posted asdfasdfasdad

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JS清除HTML标签转换成<br/;相关的知识,希望对你有一定的参考价值。

  /**
   * 把带html除了<br/>之外的清除
   * 把\\r\\n转换成</br>
   */
  function clearHtml ( html ) 
    if ( typeof html != 'string' ) 
      return '';
    
    /**
     No.  文字表記  10進表記  16進表記  文字    Comment
     001  &quot;  &#34;    &#x22;  """    quotation mark = APL quote
     002  &amp;    &#38;    &#x26;  "&"    ampersand
     003  &lt;    &#60;    &#x3C;  "<"    less-than sign
     004  &gt;    &#62;    &#x3E;  ">"    greater-than sign
     005  &nbsp;  &#160;  &#xA0;  " "    no-break space = non-breaking space
     */
    var char         = 'br';
    html             = $.trim( html );
    html             = html.replace( /(<br[^<>]*>)+/gi, char );
    html             = html.replace( /(<hr[^<>]*>)+/gi, char );
    html             = html.replace( /(<img[^<>]*>)+/gi, char );
    html             = html.replace( /(<input[^<>]*>)+/gi, char );
    html             = html.replace( /(<area[^<>]*>)+/gi, char );
    html             = html.replace( /(<base[^<>]*>)+/gi, char );
    html             = html.replace( /(<meta[^<>]*>)+/gi, char );
    html             = html.replace( /(<basefont[^<>]*>)+/gi, char );
    html             = html.replace( /(<param[^<>]*>)+/gi, char );
    html             = html.replace( /(<col[^<>]*>)+/gi, char );
    html             = html.replace( /(<frame[^<>]*>)+/gi, char );
    html             = html.replace( /(<embed[^<>]*>)+/gi, char );
    html             = html.replace( /(<keygen[^<>]*>)+/gi, char );
    html             = html.replace( /(<source[^<>]*>)+/gi, char );
    html             = html.replace(/<[^<>]+>(.*)+<[^<>]+>/gi, char + "$1" + char);
    html             = html.replace( /\\n(\\r)?/gi, char );
    html             = html.replace( /(&quot;)+/gi, '"' );
    html             = html.replace( /(&amp;)+/gi, '&' );
    html             = html.replace( /(&lt;)+/gi, '<' );
    html             = html.replace( /(&gt;)+/gi, '>' );
    html             = html.replace( /(&nbsp;)+/gi, ' ' );
    html             = html.replace( /<[^>]+>/gi, '' );
    var wrapperArray = [];
    var charArr      = html.split( char );
    for ( var i = 0, len = charArr.length; i < len; i++ ) 
      var _item = $.trim( charArr[ i ] );
      if ( _item ) 
        wrapperArray.push( _item );
      
    
    html = wrapperArray.join( '<br/>' );
    return html;
  

以上是关于JS清除HTML标签转换成<br/;的主要内容,如果未能解决你的问题,请参考以下文章

将 JSON \n 转换为 HTML <br> 标签

sql查询将换行符转换为html <br>

javascript 正则表达式 替换 html 中的 p标签替换成BR标签

.innerhtml 将 <br/> 标签转换为 <br>

HTML5/CSS/JS<br;与<p;标签区别

js时间戳怎么转成日期格式