我有一个字符串数组,其中使用了一些 HTML 代码。例如
["An ultrasound-guided biopsy was performed for the <code>[1.4 cm solid]</code> mass located in the [right ]breast, at the [12:00] position. This was described on the previous[ Ultrasound and mammography reports.] The skin was prepped in the usual manner. Local anesthetic was administered to the access site. [A guiding needle was inserted initially.] Subsequently, a [14] gauge biopsy needle was placed adjacent to the abnormality under ultrasound guidance. Once the needle was documented to be in the correct location, [4 ]core specimens were obtained using a [Bard automated firing device.] A metallic clip was inserted into the breast at the location of the biopsy. A bandage was applied to the access site. Post procedure mammographic imaging was done. The specimens were sent to the laboratory for pathological analysis. ","\n","Complications: <code>[none]</code>","\n<p></p>\n"]
我想将字符串拆分为一些模式并希望得到这个结果
[{
"text": "An ultrasound-guided biopsy was performed for the"
}, {
"text": "1.4 cm solid"
"value":"code"
},{
"text":"mass located in the [right ]breast, at the [12:00] posit… to the laboratory for pathological analysis.  "
}]
最佳答案
您可能会发现 the DOMParser
有用。
const arr = ["An ultrasound-guided biopsy was performed for the <code>[1.4 cm solid]</code> mass located in the [right ]breast, at the [12:00] position. This was described on the previous[ Ultrasound and mammography reports.] The skin was prepped in the usual manner. Local anesthetic was administered to the access site. [A guiding needle was inserted initially.] Subsequently, a [14] gauge biopsy needle was placed adjacent to the abnormality under ultrasound guidance. Once the needle was documented to be in the correct location, [4 ]core specimens were obtained using a [Bard automated firing device.] A metallic clip was inserted into the breast at the location of the biopsy. A bandage was applied to the access site. Post procedure mammographic imaging was done. The specimens were sent to the laboratory for pathological analysis. ","\n","Complications: <code>[none]</code>","\n<p></p>\n"];
var domparser = new DOMParser();
// Parse the string in the array to a new HTML document
const parsed = domparser.parseFromString(arr[0], 'text/html');
// Iterate over the child nodes with reduce to produce a new array of objects
const obj = [...parsed.body.childNodes].reduce((acc, node) => {
// Destructure the relevant props from each node
const { nodeType, textContent, tagName } = node;
// Trim the edge spaces from the text
const text = textContent.trim();
// If the nodeType is 3 (text node) return an object with
// just the text
if (nodeType === 3) return acc.concat({ text });
// Otherwise get the value
const value = tagName.toLowerCase();
// Do further string replacement
const replacedText = text.replace(/[\[\]]/g, '');
// Return the object with the text and the value
return acc.concat({ text: replacedText, value });
}, []);
console.log(obj);
进一步阅读
关于javascript - 将字符串转换为对象数组的最佳方法是什么?有具体图案吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57691029/