Guys, help me take a look at the problem of js regular reverse reference (in Wechat Mini Program)

description: the background gives a long string of strings, including text and links. At present, all I need to do is parse the string into an array like the expected result below, but there is a problem with the regular reference. Suppose there are three links in the original string. My current parsing method will cause the last problem that covers the first two links, which is not the expected result. Please, the gods have a good way to communicate or guide them. Thank you

.

direct operation of dom is not supported in WeChat Mini Programs

//
var desc = "HTTPSHTTPSHTTP" +
    "<a href="web://http://api.520.com:8891/clientpage/money.php"></a>" +
    "warningblock<a href="web://http://api.520.com:8891/activity/convention.php"></a>" +
    "HTTPHTTPS<a href="in://goplay"></a>";
    
    


//,a
/*
* "HTTPSHTTPSHTTP" +
    "<a href="https://api.520.com/clientpage/money.php?url=xxx&title=xxx"></a>" +
    "warningblock<a href="https://api.520.com/activity/convention.php?url=xxx&title=xxx"></a>" +
    "HTTPHTTPS<a href="/pages/goplay/goplay"></a>"
*
* */


//
[
    {
        "MsgType"   : "text",
        "MsgContent": "HTTPSHTTPSHTTP"
    },
    {
        "MsgType"   : "navigator",
        "MsgContent": "https://api.520.com/clientpage/money.php?url=xxx&title=xxx",
        "MsgParam":"",
    },
    {
        "MsgType"   : "text",
        "MsgContent": "warningblock"
    },
    {
        "MsgType"   : "navigator",
        "MsgContent": "https://api.520.com/activity/convention.php?url=xxx&title=xxx",
        "MsgParam":"",
    },
    {
        "MsgType"   : "text",
        "MsgContent": "HTTPHTTPS"
    },
    {
        "MsgType"   : "navigator",
        "MsgContent": "/pages/goplay/goplay",
        "MsgParam":"",
    },
]


var MsgBody = [];

var MsgContent = null,MsgType = null;

var tmsg = "", amsg, restMsgIndex = null
var expr = /<a[^>]+?href=[""]?([^""]+)[""]?[^>]*>([^<]+)<\/a>/mg
amsg = desc.match(expr); //a

console.log(amsg)
if (!amsg || amsg.length < 1) {
    MsgType = "text";
    MsgContent = desc;
    MsgBody.push({
        "MsgType"   : MsgType,
        "MsgContent": MsgContent
    })
}
else  //a
{

    var len = amsg.length; //a
    console.log(len)
    for (var i = 0; i < len; iPP) {
        tmsg = desc.substring(0, desc.indexOf(amsg[i])); //

        if (tmsg) {  //
            MsgType = "text";
            MsgContent = tmsg;
            MsgBody.push({
                "MsgType"   : MsgType,
                "MsgContent": MsgContent
            })
        }

        var pattern1 = new RegExp("in://", "ig");//,   in://mytrends

        var pattern2 = new RegExp("web://", "ig");//,  web://http://api.yuepao520.cn:8009/clientpage/friend_raiders.php

        var url = amsg[i];

        var param = "" ,src="";

        MsgType = "navigator";

        /*
            <a\b               -sharpa
            [^>]+              -sharpahref
           \bhref="([^"]*)"    -sharphref1
             [^>]*>            -sharpahref
            ([\s\S]*?)         -sharpavalue2?
            </a>               -sharpa
        */

        new RegExp("<a\b[^>]+\bhref="([^"]*)"[^>]*>([\s\S]*?)</a>", "ig");  //reg

        var link = RegExp.$1; //ahref

        var alt = RegExp.$2;//a

        console.log(url,link,alt)
        
        
        //a
        if(pattern1.test(url)) //in  <a href="in://goplay">
        {
            switch (true) {
                case new RegExp("goplay", "ig").test(link):
                    src = app.globalData.navigateTo.goPlay;
                    break;
                case new RegExp("userinfo", "ig").test(link):
                    var temp = (link).split("?");
                    src = app.globalData.navigateTo.getUserInfo+"?"+temp[1];
                    break;
                case new RegExp("setmyinfo", "ig").test(link):
                    src = app.globalData.navigateTo.setUserInfo;
                    break;
                case new RegExp("setting", "ig").test(url):

                    break;
                case new RegExp("mytrends", "ig").test(url):

                    break;
                case new RegExp("myphoto", "ig").test(url):

                    break;
                case new RegExp("visitorrecord", "ig").test(url):

                    break;
                case new RegExp("feedback", "ig").test(url):
                    src = app.globalData.navigateTo.feedBack;
                    break;
                case new RegExp("vip_pay", "ig").test(url):
                    src = app.globalData.navigateTo.vipRecharge;
                    break;
                case new RegExp("money_pay", "ig").test(url):
                    src = app.globalData.navigateTo.goldRecharge;
                    break;
                case new RegExp("payment", "ig").test(url):
                    src = app.globalData.navigateTo.goldRecharge;
                    break;
            }


        }
        else if(pattern2.test(url))//web  <a href="web://http://api.520.com:8891/activity/convention.php">
        {
            src = (link).split("//");  //https
            if(src[1].indexOf("https") == -1){
                param = src[1].replace(/http/,"https")+"//"+src[2];
                param = param.replace(/:8891/,"")
            } else {
                param = src[1]+"//"+src[2]
            }
            //H5
            //src = app.globalData.navigateTo.clientPage+"?url="+param+"&title="+alt
            src = ""+"?url="+param+"&title="+alt
        }


        MsgContent = amsg[i];

        MsgBody.push({
            "MsgType"   : MsgType,
            "MsgContent": src,
            "MsgParam":alt,
        })


        restMsgIndex = desc.indexOf(amsg[i]) + amsg[i].length;

        desc = desc.substring(restMsgIndex);

    }

    if(desc){  //
        MsgType = "text";
        MsgBody.push({
            "MsgType"   : MsgType,
            "MsgContent": desc
        })
    }
}

the subject changed the question. Pure solution, please see luckness's answer.

< hr >

? Do you have any special needs to make your own DOMParser wheels? It's not fast and bug?-free to let native code do this.

  https://developer.mozilla.org.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e4941c-44d21.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e4941c-44d21.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?