Split string and keep the separator

后端 未结 2 1105
[愿得一人]
[愿得一人] 2021-01-25 23:38

I\'m writing a chrome extension, and I need to split a string that contains only text and img tags, so that every element of the array is either letter or img tag. For example,

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-25 23:51

    You can use exec instead of split to obtain the separated elements:

    var str = 'abcd';
    var myRe = /]*>|[a-z]/gi;
    var match;
    var res= new Array();
    
    while ((match = myRe.exec(str)) !== null) {
        res.push(match[0]);
    }
    console.log(res);
    

提交回复
热议问题