2017-03-02 86 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