Guys sorry for my English it’s my second language.
So There’s a string with the following text:
**Lorem** ipsum dolor sit, amet consectetur adipisicing elit.
How to replace "**" characters with <b>
and </b>
tags or <div>
and </div>
tags with React native, so i can output it like this:
<b>Lorem</b> ipsum dolor sit, amet consectetur adipisicing elit.
I tried to start bold text with **
and end with /*
, and then replace **
with <b>
and /*
with </b>
using replace method:
str.replace("/*", "</b>").replace("**", "<b>")
but i got only string like this:
<b>Lorem</b> ipsum dolor sit, amet consectetur adipisicing elit.
.
It’s problematic because I’m using React native, which outputs only like string. It’d work in PHP.
Previously thanks!
2
Answers
You can use react-native-webview
Or any library for secure html insertion
You could do something like the below code.
Step-wise details:
**
to create an arrayarr
ofstring
.**...**
will be at odd indices inarr
array.Text
component and other strings at even indices(i.e. which are outside the**...**
block) with simpleText
component.Text
components to an array.Text
components in anotherText
component to display all of them as a single component on the same line.CodeSandbox link: https://codesandbox.io/s/happy-cori-oex9kg?file=/src/App.js
Edit: If you want the bold-text functionality to be smarter, you can check for stray texts which have dangling
**
. Code for the same:CodeSandbox link for smarter bold-text functionality: https://codesandbox.io/s/boring-haslett-238nt3?file=/src/App.js