I am trying to truncate the following html to 5 characters:
<b>Hello</b> how are you today?
The result I'm looking for is:
Hello
However, how do I ignore the html tags in the truncation so that it doesn't result in the following?
<b>He
I am using an html parser, so I can't truncate the string after using that. Here is my code which, needless to say, doesn't work!
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;
What is the correct approach?
Many thanks,
Katie
