Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Make string bold in paragraph between two delimiters javascript JSX

From DB, we are getting string in following format.

_This is string from DB_. Make sure this is format from DB

Now, on JSX/javascript, I want to make string between two underscores as bold (This is string from DB to be bold format)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use a regular expression to match the string between two underscores, and wrap it with a tag. You can use the replace method of the string object to perform this operation. For example, you can modify your code like this:

const dbString = "_This is string from DB_. Make sure this is format from DB";
const formattedString = dbString.replace(/_(.*?)_/g, "<b>$1</b>");
return <div dangerouslySetInnerHTML={{ __html: formattedString }} />;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading