简体   繁体   English

在Google Apps脚本中分割HTML标签

[英]Stripe HTML tags in Google Apps Script

I'm looking for fastets way to stripe HTML tags from content in Google Apps Script. 我正在寻找一种快速的方法来从Google Apps脚本的内容中剥离HTML标签。

For now I'm using these functions to HTML parsing: 现在,我正在使用以下功能进行HTML解析:

function getTextFromHtml(body) {
  return getTextFromNode(Xml.parse(body, true).getElement());
}

function getTextFromNode(x) {
 switch(x.toString()) {
  case 'XmlText': return x.toXmlString();
  case 'XmlElement': return x.getNodes().map(getTextFromNode).join('');
  default: return '';
 }
}

But for long HTML's this way is so inefficient. 但是对于HTML而言,这种方式是如此低效。

Sample HTML content: http://pastebin.com/FmB4hvN2 示例HTML内容: http//pastebin.com/FmB4hvN2

Any ideas? 有任何想法吗?

这将从输入中删除所有标签。

 var text = html.replace(/<[^>]+>/g, "");

If the content you want to replace is always wrapped with < and >, you can do 如果您要替换的内容始终用<和>包裹,则可以

Regex rgx = new Regex(someString);
string result = rgx.Replace("<[^>]*>", "");

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

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