【逐日一包0016】striptags

[github地点:https://github.com/ABCDdouyae…]

striptags

剥去字符串中的html标签,和php内里的strip_tags函数一样

  • 一般用法

用法:striptags(html, allowed_tags, tag_replacement)

返回:string

const striptags = require('striptags');

var html =
    '<a href="https://example.com">' +
        'lorem ipsum <strong>dolor</strong> <em>sit</em> amet' +
    '</a>';
 
var a = striptags(html);// 'lorem ipsum dolor sit amet'
var b = striptags(html, '<strong>');//'lorem ipsum <strong>dolor</strong> sit amet'
var c = striptags(html, ['a']);//<a href="https://example.com">lorem ipsum dolor sit amet</a>
var d = striptags(html, [], '\n');
/** 
 * lorem ipsum
 * dolor
 * sit
 * amet
 */
var e = striptags(html, ['em']);// lorem ipsum dolor <em>sit</em> amet
  • 流形式[初始化一个流形式,传入html]

用法:striptags.init_streaming_mode(allowed_tags, tag_replacement)

返回:string

let stream_function = striptags.init_streaming_mode(['strong'], '$$');
console.log(stream_function(html));
//$$lorem ipsum <strong>dolor</strong> $$sit$$ amet$$
    原文作者:王炜
    原文地址: https://segmentfault.com/a/1190000017140286
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞