2017-03-02 63 views
0

在这个例子中,内容对象有我不想保存的html代码。如何将内容对象作为文本保存在这个json文件中?我想用node.js保存在documentdb中。如何用Javascript删除HTML代码?

{ 
     "title": "Azure IoT Suite adds device management capability updates", 
     "content": "<p>While the most successful enterprise IoT solutions include a strategy for operators to handle ongoing management of device collection in a simple and reliable manner, it can be a hurdle for companies getting started with IoT. To help with that challenge, we recently <a href=\"https://azure.microsoft.com/en-us/blog/enhancements-abound-in-general-availability-of-azure-iot-hub-device-management/\">introduced</a> device management capabilities in Azure IoT Hub.</p> <p>Today, we&rsquo;ve added these device management features to the <a href=\"https://azure.microsoft.com/en-us/blog/microsoft-azure-iot-suite-connecting-your-things-to-the-cloud/\">Azure IoT Suite</a> remote monitoring preconfigured solution. The Azure IoT Suite simplifies deploying and orchestrating advanced services to give businesses a complete IoT solution from proof of concept to broader deployment.</p> <p>With new device management functionality in Azure IoT Suite, developers will be able to quickly move beyond telemetry processing, rule management, and visualization to customize their device overview, queries and device lists. These enhancements include:</p> <ul> <li>Synchronizing settings and metadata between the cloud and devices using <strong>device twins</strong>.</li> <li>Performing an action on a connected device through the cloud using <strong>direct methods</strong>.</li> <li>Broadcasting and orchestrating operations on multiple devices at a planned time through <strong>jobs</strong>.</li> <li>Attesting the status and health for on or offline device collections using real-time, dynamic <strong>queries</strong> across device twins and jobs.</li> </ul> <p><a href=\"https://azurecomcdn.azureedge.net/mediahandler/acomblog/media/Default/blog/2eff2e8f-2e3c-47d0-a504-f3482a1f87ef.png\"><img alt=\"Contoso MD-1 Devices\" border=\"0\" height=\"134\" src=\"https://azurecomcdn.azureedge.net/mediahandler/acomblog/media/Default/blog/f007d7e1-909d-4831-af81-f5f12ce33ef6.png\" style=\"border-width: 0px; padding-top: 0px; padding-right: 0px; padding-left: 0px; margin-right: auto; margin-left: auto; float: none; display: block; background-image: none;\" title=\"Contoso MD-1 Devices\" width=\"480\"></a></p> <ul> <li>Customizing device information overview by using <strong>Column Editor</strong> to provide a dynamic report for devices you want to monitor right now.</li> </ul> <p><a href=\"https://azurecomcdn.azureedge.net/mediahandler/acomblog/media/Default/blog/a8e182e4-e16d-42ad-8e0b-2791e121e096.png\"><img alt=\"Costoso MD-1 Devices Edit Columns\" border=\"0\" height=\"239\" src=\"https://azurecomcdn.azureedge.net/mediahandler/acomblog/media/Default/blog/a7ce0f2b-d1c2-4afb-a9e5-25d2f7bcd55f.png\" style=\"border-image: none; padding-top: 0px; padding-right: 0px; padding-left: 0px; margin-right: auto; margin-left: auto; float: none; display: block; background-image: none;\" title=\"Costoso MD-1 Devices Edit Columns\" width=\"480\"></a></p> <p>The Azure IoT Suite remote monitoring preconfigured solution is also <a href=\"https://github.com/Azure/azure-iot-remote-monitoring\">open source</a>, which gives developers the flexibility to customize it to their needs as the business evolves. We are excited to see developers achieve even more through the new device management features.</p> <p>Learn more about today&rsquo;s enhancements by reviewing our two step-by-step guides: <a href=\"https://docs.microsoft.com/azure/iot-suite/iot-suite-getstarted-preconfigured-solutions\">Get started with the preconfigured solutions</a> and <a href=\"https://docs.microsoft.com/azure/iot-suite/iot-suite-remote-monitoring-sample-walkthrough\">Remote monitoring preconfigured solution walkthrough</a>. You can also provision your IoT solution with your Azure subscription today by visiting <a href=\"http://www.azureiotsuite.com/\">www.azureiotsuite.com</a>.</p>", 
     "published": "2017-02-28T11:00:06.000Z", 
     "author": "Sam George", 
     "link": "https://azure.microsoft.com/blog/azure-iot-suite-adds-device-management-capability-updates/", 
     "feed": { 
     "source": "https://azure.microsoft.com/en-us/blog/feed/", 
     "link": "https://azure.microsoft.com/blog/", 
     "name": "Microsoft Azure Blog" 
     }, 
     "id": "ae8e3449-7d07-bd0b-1156-43cfe4a0a75a" 
    } 

有了这个代码,我可以摆脱html标签的,但也有像“我们& rsquo的”如此多的HTML代码。

articles.forEach(function (element) { 

     element.content = element.content.replace(/<{1}[^<>]{1,}>{1}/g," "); 

    }); 

有没有什么方法可以保存这个内容没有html代码和标签?

+0

这可能有所帮助:http://stackoverflow.com/问题/ 822452/strip-html-from-text-javascript – mvermand

+0

不太确定为什么你会尝试在事物的客户端做到这一点。我认为大多数服务器端语言都有自动化的功能(例如PHP中的['strip_tags()'](http://php.net/manual/en/function.strip-tags.php))。另外,你有什么尝试,它失败的地方,你期望什么以及你目前的代码是什么?你读过[如何问](http://stackoverflow.com/help/how-to-ask)? – Nukeface

+0

我想用node.js保存在documentdb中。我collega问我,如果我的HTML可以解析和保存。 –

回答

1

创建一个元素,将HTML的内容,并设置内容所产生的innerText:

articles.forEach(v => { 
    let tmp = document.createElement('div'); 
    tmp.innerHTML = v.content; 
    v.content = tmp.innerText; 
}); 

var articles = [{ 
 
     "title": "Azure IoT Suite adds device management capability updates", 
 
     "content": "<p>While the most successful enterprise IoT solutions include a strategy for operators to handle ongoing management of device collection in a simple and reliable manner, it can be a hurdle for companies getting started with IoT. To help with that challenge, we recently <a href=\"https://azure.microsoft.com/en-us/blog/enhancements-abound-in-general-availability-of-azure-iot-hub-device-management/\">introduced</a> device management capabilities in Azure IoT Hub.</p> <p>Today, we&rsquo;ve added these device management features to the <a href=\"https://azure.microsoft.com/en-us/blog/microsoft-azure-iot-suite-connecting-your-things-to-the-cloud/\">Azure IoT Suite</a> remote monitoring preconfigured solution. The Azure IoT Suite simplifies deploying and orchestrating advanced services to give businesses a complete IoT solution from proof of concept to broader deployment.</p> <p>With new device management functionality in Azure IoT Suite, developers will be able to quickly move beyond telemetry processing, rule management, and visualization to customize their device overview, queries and device lists. These enhancements include:</p> <ul> <li>Synchronizing settings and metadata between the cloud and devices using <strong>device twins</strong>.</li> <li>Performing an action on a connected device through the cloud using <strong>direct methods</strong>.</li> <li>Broadcasting and orchestrating operations on multiple devices at a planned time through <strong>jobs</strong>.</li> <li>Attesting the status and health for on or offline device collections using real-time, dynamic <strong>queries</strong> across device twins and jobs.</li> </ul> <p><a href=\"https://azurecomcdn.azureedge.net/mediahandler/acomblog/media/Default/blog/2eff2e8f-2e3c-47d0-a504-f3482a1f87ef.png\"><img alt=\"Contoso MD-1 Devices\" border=\"0\" height=\"134\" src=\"https://azurecomcdn.azureedge.net/mediahandler/acomblog/media/Default/blog/f007d7e1-909d-4831-af81-f5f12ce33ef6.png\" style=\"border-width: 0px; padding-top: 0px; padding-right: 0px; padding-left: 0px; margin-right: auto; margin-left: auto; float: none; display: block; background-image: none;\" title=\"Contoso MD-1 Devices\" width=\"480\"></a></p> <ul> <li>Customizing device information overview by using <strong>Column Editor</strong> to provide a dynamic report for devices you want to monitor right now.</li> </ul> <p><a href=\"https://azurecomcdn.azureedge.net/mediahandler/acomblog/media/Default/blog/a8e182e4-e16d-42ad-8e0b-2791e121e096.png\"><img alt=\"Costoso MD-1 Devices Edit Columns\" border=\"0\" height=\"239\" src=\"https://azurecomcdn.azureedge.net/mediahandler/acomblog/media/Default/blog/a7ce0f2b-d1c2-4afb-a9e5-25d2f7bcd55f.png\" style=\"border-image: none; padding-top: 0px; padding-right: 0px; padding-left: 0px; margin-right: auto; margin-left: auto; float: none; display: block; background-image: none;\" title=\"Costoso MD-1 Devices Edit Columns\" width=\"480\"></a></p> <p>The Azure IoT Suite remote monitoring preconfigured solution is also <a href=\"https://github.com/Azure/azure-iot-remote-monitoring\">open source</a>, which gives developers the flexibility to customize it to their needs as the business evolves. We are excited to see developers achieve even more through the new device management features.</p> <p>Learn more about today&rsquo;s enhancements by reviewing our two step-by-step guides: <a href=\"https://docs.microsoft.com/azure/iot-suite/iot-suite-getstarted-preconfigured-solutions\">Get started with the preconfigured solutions</a> and <a href=\"https://docs.microsoft.com/azure/iot-suite/iot-suite-remote-monitoring-sample-walkthrough\">Remote monitoring preconfigured solution walkthrough</a>. You can also provision your IoT solution with your Azure subscription today by visiting <a href=\"http://www.azureiotsuite.com/\">www.azureiotsuite.com</a>.</p>", 
 
     "published": "2017-02-28T11:00:06.000Z", 
 
     "author": "Sam George", 
 
     "link": "https://azure.microsoft.com/blog/azure-iot-suite-adds-device-management-capability-updates/", 
 
     "feed": { 
 
     "source": "https://azure.microsoft.com/en-us/blog/feed/", 
 
     "link": "https://azure.microsoft.com/blog/", 
 
     "name": "Microsoft Azure Blog" 
 
     }, 
 
     "id": "ae8e3449-7d07-bd0b-1156-43cfe4a0a75a" 
 
    }]; 
 
    
 
articles.forEach(v => { 
 
    let tmp = document.createElement('div'); 
 
    tmp.innerHTML = v.content; 
 
    v.content = tmp.innerText; 
 
}); 
 

 
console.log(articles);

用于node.js中,你可以使用cheerio

const cheerio = require("cheerio"); 
articles.forEach(v => { 
    v.content = cheerio.load(v.content).text(); 
}); 

console.log(articles); 
+0

嗨@baao这给node.js中的错误。我应该安装任何软件包吗?它说ReferenceError:文档没有定义。 –

+0

你应该在你的问题中指定你想在节点而不是浏览器中使用它。 @AttilaAlan你可以在节点中用cheerio试试它:https://github.com/cheeriojs/cheerio – baao

+0

用cheerio @AttilaAlan的编码编辑我的答案 – baao