How to split a js string into an array of specified tags

for example:
var str = "I am a good friend of / @ Xiao Wang @\\ and / @ Xiao Li @\\"
becomes
var arr = ["I am", "/ @ Xiao Wang @\\", "and", "/ @ Xiao Li @\\", "good friend"]
I try str.split (/\ / @ | @\\ /) to get "good friend of"]
needs to keep the label and order to distinguish. It is troublesome for split regular tags to be removed.
it would be better if
var arr = [{"text": "I am"}, {"name": Xiao Wang "}, {" text ":" and "}, {" name ":" Xiao Li "}, {" text ":" good friend "}]

Mar.18,2021

in fact, this problem is very simple, just analyze it. The analysis is as follows:

var str = "/@@\\/@@\\"

you want to switch to

  var arr = [{"text": ""}, {"name": "}, {"text": ""}, {"name": ""}, {"text": ""}]

as far as I'm concerned, the string is split by / @ and @\, and the string that ends with / @ is put in the text attribute, and the one that ends with @\ is put in the name, and the original order is maintained.
since there are two split characters, let's split them twice. I wrote it briefly, but I didn't do much testing and judgment. You will check the parameters later.

my code is as follows:

var str = "/@@\\/@@\\";
    var strs = str.split("/@");
    var arr = new Array();
    for (var i = 0 ;i < strs.length;iPP)
    {
        if(strs[i].indexOf('@\\') != -1)
        {
            var temps = strs[i].split('@\\');
            for(var j=0;j<temps.length;jPP)
            {
                if(j == temps.length -1)
                {
                    var text =
                    {
                        text:temps[j]
                    }
                }else
                {
                    var name =
                    {
                        name:temps[j]
                    }
                }
            }
        }else
        {
            var text =
            {
                text:strs[i]
            }
        }
        if(name != null && '' != name && 'undifined' != name)
        {
            arr.push(name);
        }
        if(text != null && '' != text && 'undifined' != text)
        {
            arr.push(text);
        }
    }
    console.log(arr);
:

if there is no problem, please accept it, thank you.


another simple solution to my manual replace is to add an extra @ like this / @ @ Xiaoming @\ , which is identified with the str.split (/\ / @) division.

Menu