【发布时间】:2021-04-14 01:23:18
【问题描述】:
我正在尝试将以下 html 截断为 5 个字符:
<b>Hello</b> how are you today?
我要找的结果是:
你好
但是,如何忽略截断中的 html 标签,使其不会导致以下结果?
<b>He
我使用的是 html 解析器,所以在使用后我无法截断字符串。这是我的代码,不用说,它不起作用!
import React from 'react';
import parse from 'html-react-parser';
import Typography from '@material-ui/core/Typography';
const Message= () => {
const message= "<b>Hello</b> how are you today?"
const messageParsed = parse(message);
return (
<Typography variant="body2">
{messageParsed.substr(0, 5)}
</Typography>
);
};
export default Message;
正确的做法是什么?
非常感谢,
凯蒂
【问题讨论】:
标签: reactjs html-parsing truncate