从字符串中删除html实体以修复JSON ERROR中的意外标记

问题描述 投票:0回答:2

如何删除所有“\ n”和空格以及“&nbsp”之类的内容:

我尝试了这个,但它没有做到这一点:x.replace(/\\/g, '').replace(' ', '').replace('\n', '');

javascript json regex angular html-entities
2个回答
1
投票

使用正则表达式

console.log( x.replace(/(&nbsp;|<([^>]+)>)/ig, "") );

 var x = `{    \n    \"formType\":\"CreateCaseRequest\",\n    \"documentID\":270550224,\n    \"documentRev\":\"1\",\n    \"formVersion\":\"v1\",\n    \"createdDateTime\":\"2019-03-25T13:31:44.216+0000\",\n    \"documentStatus\":\"Draft\",\n    \"documentTitle\":\n    {\n    },\n    \"documentSynopsis\":\n    {\n    },\n    \"highPriority\":false,\n   }`

console.log( x.replace(/(&nbsp;|<([^>]+)>)/ig, "") );

1
投票

试试这个。

x.replace(“\ n”,“\ n”);

© www.soinside.com 2019 - 2024. All rights reserved.