简体   繁体   English

正则表达式/JS – 从特定 xml 标记中删除内容并仅保留文本

[英]Regular Expression/JS – remove content from specific xml tags and keep only text

I'd like to remove content from specific tags and keep only the text.我想从特定标签中删除内容并只保留文本。 I'm using Javascript but on React Native, so I can't use Dom or JQuery.我正在使用 Javascript 但在 React Native 上,所以我不能使用 Dom 或 JQuery。

For example, for this input, I'd like to remove all the sub-tags of "li":例如,对于这个输入,我想删除“li”的所有子标签:

<div>
     <div><span>hello</span></div>  
     <ul>
         <li><div><span>hello 2</span></div></li>
         <li><div><span>hello 3</span></div></li>
     </ul>       
</div>

I'd like to get this output:我想要这个 output:

<div>
     <div><span>hello</span></div>  
     <ul>
         <li>hello 2</li>
         <li>hello 3</li>
     </ul>       
</div>

How can I achieve this?我怎样才能做到这一点? (again, not using DOM or JQuery) (同样,不使用 DOM 或 JQuery)

Thank you!谢谢!

Here is a native JavaScript way to filter out specific tags within <li> tags:这是一个原生的 JavaScript 方法来过滤掉<li>标签中的特定标签:

 var str = '<div>\n' + ' <div><span>hello</span></div>\n' + ' <ul>\n' + ' <li><div><span>hello 2</span></div></li>\n' + ' <li><div><span>hello 3</span></div></li>\n' + ' </ul>\n' + '</div>'; var re1 = /(<li> *)(.*?)(<\/li>)/g; var re2 = /<\/?(div|span)\b[^>]*> */g; var result = str.replace(re1, function(m, p1, p2, p3) { return p1 + p2.replace(re2, '') + p3; }); console.log('result:\n' + result);

Console output:控制台 output:

result:
<div>
     <div><span>hello</span></div>
     <ul>
         <li>hello 2</li>
         <li>hello 3</li>
     </ul>
</div>

Explanation:解释:

  • re1 identifies <li> tags re1标识<li>标签
    • it has the g flag to match multiple times它有多次匹配的g标志
    • the replace function has three capture groups as parameters p1 , p2 , p3替换 function 具有三个捕获组作为参数p1p2p3
  • the replace function removes all div and span tags from p2 , which is the content within the <li> tag替换 function 从p2中删除所有divspan标签,这是<li>标签中的内容
    • tweak the re2 to add additional tags to filter调整re2以添加额外的标签来过滤
    • it also has the g flag to match multiple times它还具有多次匹配的g标志

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM