解析带emoji和连接的聊天系统消息

在写聊天系统的时候,不可避免地要对聊天系统中的消息作一些解析
常见的好比一句话中带有emoji、link等信息的时候,要把emoji解析成图片、把link转成能够点击的
(项目中没有作对图片作行内处理,而是把图片像微信同样做为单独消息发送)
咱们知道react的标签都是jsx的,因此在解析消息的时候,就必须在获得消息内容的时候,就先把消息内容分段截取
好比这样一则消息react

今天吃饭了吗?[emoji]我还没吃呢[emoji],给你个连接看看吧!http://www.google.com/

emoji要解析成图片,http://www.google.com/ 要解析成能够点击的连接,之间的文字要解析成文本jquery

jquery时代,只须要使用正则匹配emoji,替换成图片,用正则匹配连接,替换成a标签便可
可是在react里,这三者对应的是不一样的jsx标签。因此必须把文本解析成分段式的数组

思路:
上面这句话,能够解析成6部分微信

part1: 今天吃饭了吗?
part2: [emoji]
part3: 我还没吃呢
part4: [emoji]
part5: ,给你个连接看看吧!
part6: http://www.google.com/

每部分对应使用不一样的jsx标签google

第一步,咱们先使用正则匹配emoji和连接
分别的正则以下
(匹配连接应该有更优秀的正则)url

var emojiregex = new RegExp(/\ud83c[\udf00-\udfff]|\ud83d[\udc00-\ude4f]|\ud83d[\ude80-\udeff]/g, 'g'); // 匹配emoji字符
var matchUrlRegex = new RegExp(/(https?:)\/\/([^\/]+)(\/[^\?]*)?(\?[^#]*)?(#.*)?/g,'g'); // 匹配url的正则

var emojiRegArray = text.match(emojiregex); // 匹配了全部的emoji的词
var urlRegArray = text.match(matchUrlRegex);

获得两个数组,分别是匹配到的emoji和匹配到的url3d

第二步,使用index()方法,获取每一个emoji、url的位置、长度,并记录指针

var indexEmojiArray = []; // 记录表情的位置、内容的数组
    var indexUrlArray = []; // 记录连接的位置、内容的数组
    
    var pos1 = -1, pos2 = -1;//头
    if(emojiRegArray){
        for (let i = 0; i < emojiRegArray.length; i++) {
            pos1 = text.indexOf(emojiRegArray[i], pos1 + 1);
            indexEmojiArray.push({
                type: 1, // type为1表示是表情
                pos: pos1,
                length: emojiRegArray[i].length,
                res: emojiRegArray[i],
            });
        }
    }
    if(urlRegArray){
        for (let i = 0; i < urlRegArray.length; i++) {
            pos2 = text.indexOf(urlRegArray[i], pos2 + 1);
            indexUrlArray.push({
                type: 3, // type为1表示是url
                pos: pos2,
                length: urlRegArray[i].length,
                res: urlRegArray[i],
            });
        }
    }

第三步,按照这些元素在消息中的位置,两个数组合并成一个数组code

// 合并两个数组
    var indexArray = []; // 以上两个数组按照pos顺序合并的数组
    if(emojiRegArray && urlRegArray){
        let point1 = 0,point2 = 0;
        while(point1 < indexEmojiArray.length || point2 < indexUrlArray.length){
            if(!indexEmojiArray[point1]){ // emoji加完了
                indexArray.push(indexUrlArray[point2]);
                point2++;                
            }else if(!indexUrlArray[point2]){// url加完了
                indexArray.push(indexEmojiArray[point1]);
                point1++;
            }else{ // 两个都没加完
                if(indexEmojiArray[point1].pos < indexUrlArray[point2].pos){
                    indexArray.push(indexEmojiArray[point1]);
                    point1++;
                }else{
                    indexArray.push(indexUrlArray[point2]);
                    point2++;
                }
            }
        }
    }else if(emojiRegArray && !urlRegArray){ // 有emoji没有url
        indexArray = indexEmojiArray;
    }else if(!emojiRegArray && urlRegArray){ // 有url没有emoji
        indexArray = indexUrlArray;
    }

第四步
如今,咱们获得了一个indexArray,存储了emoji和url的位置和长度的数组
如今咱们要把文本也加进去,而且,emoji替换成图片对象

// 这里开始把indexArray加工成contentArray
    let contentArray = [];
    let point = 0; // 记录当前指针位置
    for (let i = 0; i < indexArray.length; i++) {
        // 把这一项和上一项之间的内容push成文本
        console.log(point);
        let textContent = text.substr(point, indexArray[i].pos-point);
        console.log(textContent);
        contentArray.push({type: 0, content: textContent});
        // point += textContent.length;
        if(indexArray[i].type === 1){ // 若是这一项是emoji
            // contentArray.push({ type: 1, "resources": EMOJI_MAP[indexArray[i].res] || [] });
            contentArray.push({ type: 1, resources: indexArray[i].res || [] });
            point = indexArray[i].pos + indexArray[i].length;
        }else if(indexArray[i].type === 3){ // 若是这一项是url
            contentArray.push({ type: 3, url: indexArray[i].res});
            point = indexArray[i].pos + indexArray[i].length;
        }
    }
    // 加入末尾项。若是indexArray为空,那么末尾项就是惟一的文本项
    let lastPrevItemIndex = (indexArray[indexArray.length-1] ? indexArray[indexArray.length-1].pos+indexArray[indexArray.length-1].length : 0);
    contentArray.push({type: 0, content: text.substr(lastPrevItemIndex, text.length)});

最后获得的contentArray咱们return出去。

比较难的部分在第四步,思路是,咱们使用一个指针,对消息作解析,一开始指针的位置为0
开头无论如何都push一个文本对象进入contentArray中
直到遇到emoji或者url位置为止
好比遇到的是emoji,咱们把emoji解析成对象push到contentArray中,而后指针加上emoji的长度
最后加上末尾。若是末尾不为文本,也添加一个空的文本对象。

完毕。

下面附上全部代码

let stringToContentArray = function (text) {


    var emojiregex = new RegExp(/\ud83c[\udf00-\udfff]|\ud83d[\udc00-\ude4f]|\ud83d[\ude80-\udeff]/g, 'g');
    var matchUrlRegex = new RegExp(/(https?:)\/\/([^\/]+)(\/[^\?]*)?(\?[^#]*)?(#.*)?/g,'g'); // 匹配url的正则
    var contentArray = [];
    if (!text) { // 没有内容
        contentArray.push({ type: 0, "content": '[无内容消息]' });
        return contentArray;
    }
    var emojiRegArray = text.match(emojiregex); // 匹配了全部的emoji的词
    var urlRegArray = text.match(matchUrlRegex);
    // console.log(text);
    console.log('emojiRegArray:',emojiRegArray);
    console.log('urlRegArray:',urlRegArray);
    if (emojiRegArray === null && urlRegArray === null) { // 没有emoji表情, 也没有连接
        contentArray.push({ type: 0, "content": text });
        return contentArray;
    }
    

    var indexEmojiArray = []; // 记录表情的位置、内容的数组
    var indexUrlArray = []; // 记录连接的位置、内容的数组
    var indexArray = []; // 以上两个数组按照pos顺序合并的数组
    var pos1 = -1, pos2 = -1;//头
    if(emojiRegArray){
        for (let i = 0; i < emojiRegArray.length; i++) {
            pos1 = text.indexOf(emojiRegArray[i], pos1 + 1);
            indexEmojiArray.push({
                type: 1, // type为1表示是表情
                pos: pos1,
                length: emojiRegArray[i].length,
                res: emojiRegArray[i],
            });
        }
    }
    if(urlRegArray){
        for (let i = 0; i < urlRegArray.length; i++) {
            pos2 = text.indexOf(urlRegArray[i], pos2 + 1);
            indexUrlArray.push({
                type: 3, // type为1表示是url
                pos: pos2,
                length: urlRegArray[i].length,
                res: urlRegArray[i],
            });
        }
    }
    if(emojiRegArray && urlRegArray){
        let point1 = 0,point2 = 0;
        while(point1 < indexEmojiArray.length || point2 < indexUrlArray.length){
            if(!indexEmojiArray[point1]){ // emoji加完了
                indexArray.push(indexUrlArray[point2]);
                point2++;                
            }else if(!indexUrlArray[point2]){// url加完了
                indexArray.push(indexEmojiArray[point1]);
                point1++;
            }else{ // 两个都没加完
                if(indexEmojiArray[point1].pos < indexUrlArray[point2].pos){
                    indexArray.push(indexEmojiArray[point1]);
                    point1++;
                }else{
                    indexArray.push(indexUrlArray[point2]);
                    point2++;
                }
            }
        }
    }else if(emojiRegArray && !urlRegArray){ // 有emoji没有url
        indexArray = indexEmojiArray;
    }else if(!emojiRegArray && urlRegArray){ // 有url没有emoji
        indexArray = indexUrlArray;
    }

    console.log("indexArray: ", indexArray);

    // 这里开始把indexArray加工成contentArray
    let point = 0; // 记录当前指针位置
    for (let i = 0; i < indexArray.length; i++) {
        // 把这一项和上一项之间的内容push成文本
        console.log(point);
        let textContent = text.substr(point, indexArray[i].pos-point);
        console.log(textContent);
        contentArray.push({type: 0, content: textContent});
        // point += textContent.length;
        if(indexArray[i].type === 1){ // 若是这一项是emoji
            // contentArray.push({ type: 1, "resources": EMOJI_MAP[indexArray[i].res] || [] });
            contentArray.push({ type: 1, resources: indexArray[i].res || [] });
            point = indexArray[i].pos + indexArray[i].length;
        }else if(indexArray[i].type === 3){ // 若是这一项是url
            contentArray.push({ type: 3, url: indexArray[i].res});
            point = indexArray[i].pos + indexArray[i].length;
        }
    }
    // 加入末尾项。若是indexArray为空,那么末尾项就是惟一的文本项
    let lastPrevItemIndex = (indexArray[indexArray.length-1] ? indexArray[indexArray.length-1].pos+indexArray[indexArray.length-1].length : 0);
    contentArray.push({type: 0, content: text.substr(lastPrevItemIndex, text.length)});
    
    return contentArray;
}
相关文章
相关标签/搜索