Home > Mobile >  How to replace space with   in a string (ReactJS) and render in HTML?
How to replace space with   in a string (ReactJS) and render in HTML?

Time:11-01

<div className="mt-2 font-sidebar capitalize">
  {item.title}
</div>

item.title can be any string (from backend) for example "all products", "most liked", "featured items", etc.

I want a way to replace the space in item.title with &nbsp; so that when rendered in HTML it still has a space - This is for a specific use case.

I would really appreciate your help.

CodePudding user response:

You can do like this:

{item.title.replaceAll(' ', '\u00a0')};

you can use replace() if you are sure there is only 1 space, so i suggest to use replaceAll()

Instead of using the   you can use the Unicode character which &nbsp; refers to (U 00A0):

  • Related