Recently I have been coding the WeChat mini program.
There are some HTML strings will get from the remote server (these strings are also used by the website, so it has to convert by JavaScript) and render into the body of the mini program.
Whereas, WeChat mini program cannot accept the HTML strings that I have to convert it.
For example, the HTML strings are like this:
<div>
<h2 id="Title">Here is a H2</h2>
<article class="ContentArticle">
There are some articles.
</article>
</div>
I have to convert them like this:
<view class="div">
<text class="Title h2">Here is a H2</text>
<text class="ContentArticle article">
There are some articles.
</text>
</view>
In my first opinion, I consider achieving this by String.replace
method. However, it can hardly match the class
and id
.
How can I achieve this by using the original JavaScript (the WeChat mini program does not support any other library such as jQuery)?