将一段html代码转成纯文本
正则:/<[^>]*>|/g
<div class="rich-text-container">111! !!123</div>↵<div class="rich-text-container">!222</div>↵<h1 class="rich-text-container">123</h1>↵<div class="rich-text-container"> </div>'.replace(/<[^>]*>|/g, '')
结果:"111! !!123↵!222↵123↵ "
将一段html代码的纯文本转换成纯文本
正则:/(?<=>)(.|\s)?(?=</?\w+[^<]>)/g
'<div class="rich-text-container">111! !!123</div>↵<div class="rich-text-container">!222</div>↵<h1 class="rich-text-container">123</h1>↵<div class="rich-text-container"> </div>'.match(/(?<=>)(.|\s)*?(?=<\/?\w+[^<]*>)/g,'')
结果:["111! !!123", "↵", "!222", "↵", "123", "↵", " "]
问题:js不支持正则表达式不支持前瞻后顾
解决方法 正则换成 /(?=>)(.|\s)?(?=</?\w+[^<]>)/g
'<div class="rich-text-container">111! !!123</div>↵<div class="rich-text-container">!222</div>↵<h1 class="rich-text-container">123</h1>↵<div class="rich-text-container"> </div>'.match( /(?=>)(.|\s)*?(?=<\/?\w+[^<]*>)/g,'')
结果:[">111! !!123", ">↵", ">!222", ">↵", ">123", ">↵", "> "]
问题:都会带一个>号,map循环slice下就好了