search

Home  >  Q&A  >  body text

Make string in paragraph between two delimiters bold javascript JSX

From the database, we get the string in the following format.

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

Now, on JSX/javascript, I want to set the string between two underscores to bold (this is the string from the database in bold format)

P粉294954447P粉294954447426 days ago563

reply all(1)I'll reply

  • P粉573943755

    P粉5739437552023-09-16 00:28:10

    You can use a regular expression to match a string between two underscores and wrap it with a label. You can do this using the string object's replace method. For example, you can modify the code like this:

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

    reply
    0
  • Cancelreply