我有一个 api 响应,它是纯文本和图像标签的组合。将纯文本与响应分开的最佳方法是什么?我需要分隔纯文本,以便我可以单独设置样式。
这是响应的示例:
This is the cap you unscrew to open when you refuel your car <img alt="blah" src="https://www.lifesure.co.uk/cms/wp-content/uploads/2011/04/Tyre-Illustration-500.jpg" />
您可以使用正则表达式删除这样的 html 标签
str = str.replace(/<(?:.|\n)*?>/gm, '');
或者你这个如果只删除图像标签 str = str.replace(/<img .*?>/g, '');
var str = 'This is the cap you unscrew to open when you refuel your car <img alt="blah" src="https://www.lifesure.co.uk/cms/wp-content/uploads/2011/04/Tyre-Illustration-500.jpg" />'
//str = str.replace(/<(?:.|\n)*?>/gm, '');
str = str.replace(/<img .*?>/g, '');
console.log(str);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句