Currently, I am developing an innovative in-game text app for the popular game Mobile Legends. This app allows users to customize the color of their chat texts during gameplay. In addition to changing colors, users can also apply other styling options such as bold, italic, underline, and strike-through, resulting in a code that can be directly inputted into the game. The app is designed with user-friendliness and speed in mind, but I am facing some challenges when it comes to the creative aspects.
The website:
I have developed my own rich editor utilizing the execcommand
feature. However, I need a solution that can convert the HTML format generated by the rich editor into the text format compatible with Mobile Legends.
The Text Format in Mobile Legends
The formatting in Mobile Legends mirrors that of HTML. For instance, instead of using <b>Hello World</b>
, it utilizes brackets like [b]Hello World[/b]
.
These are some common text formats in Mobile Legends which closely resemble HTML:
Bold: [b]Hello World[/b]
Italic: [i]Hello World[/i]
Underline: [u]Hello World[/u]
StrikeThrough: [s]Hello World[/s]
ForeColor: [FF0000]Hello World
(uses hex code)
The Issue at Hand
The challenge is how to convert this code (example output):
Hello World!
<b><i><u><strike>Hello World!</strike></u></i></b>
into this format:
[b][i][u][s]Hello World![/b][/i][/s]
Proposed Solution
I have attempted to use the method of .replace(/<b>/g,'[b]')
. However, as the formatting becomes more complex, replacing the tags becomes increasingly difficult. This leads to complicated code with numerous attributes.
Example of a complex format:
Lorem ipsum dolor sit
amet.
<span style="font-family: "Open Sans", Arial, sans-serif; font-size: 14px; text-align: justify; background-color: rgb(255, 255, 255);"><span style="text-decoration: underline;"><font color="#a9fcd4"><i>Lo</i><b>rem</b></font></span> <i style="text-decoration: underline;"><font color="#0000cc">ipsu<b>m</b></font></i> <b style="text-decoration: underline;"><i><font color="#cc00cc">dolor</font></i></b> <u><font color="#cccc00">sit</font></u> <strike><font color="#cc00cc"><b>amet</b><i>.</i></font></strike></span><br>
Disclaimer: I do not claim to be a professional coder, so I apologize for any novice errors in my work. Coding is simply a hobby of mine, and I decided to create this app for the Mobile Legends community since I enjoy playing the game and wanted to enhance my bio and chat text.