I need to format text based on provided text specific style using react js. e.g:
- If text contains this: nBubblenAirnSKY
Output: Bubble<br/>Air<br/>SKY<br/>
- If text contains like this: n-Bubblen-Airn-SKY
Output :
- Bubble
- Air
- SKY
Here 1 hyphen (-) = one unordered list
- If text contains like this: n#Bubblen#Airn#SKY
Output:
1.Bubble
2.Air
3.SKY
- If text contains like this: n-Bubblen–Airn—SKYn—-Cloud
Output:
- Bubble
- Air
- SKY
- Cloud
- SKY
- Air
In the above text, Each hyphen increase means it should be the child unordered list according to their parent list item.
I need to show the output using html based on text condition
I have seen in XAML textbox (windows) if i add the text with above style text behaves like as shown in output.
What i am tried to find the n and replace with <br/> tag.
var text = "nBubblenAirnSKY"
var newChar = new RegExp(/^[S.]+$/g);
var isNewLineExist = newChar.test(text);
if(isNewLineExist) //this is working fine if string contains newline character
console.log('newline exist in string');
else
console.log('newline does not exist in string');
//But below code for replace is not working. Where i tried to replace n with <br/> based on regex
var replaceChar = text.replace(/^[n.]/g, "<br/>");
console.log(replaceChar);
return replaceChar;
//Looking for other output also.
I have tried with other regex then include() but they are not able to find if string contains n character.
Need help
2
Answers
You can create a component in React js for this and you can reuse it.
You might need to handle different cases using regular expressions and conditionally render the HTML. I would do it like following:
So, here
FormattedText
component usesdangerouslySetInnerHTML
to display the HTML components.