鍍金池/ 問答/HTML/ js切割array中的字串報錯

js切割array中的字串報錯

let str = "http://www.eee.com/dqwe/dd/ccc.jpghttp://www.qqe.com/qwe/qwe.JPG"

console.log(str.match(/(?<=^|.jpg)(.+?).jpg/g));

需求是要切割array中的網址

使用match(/(?<=^|.jpg)(.+?).jpg/ig) 切割

在chrome沒問題 但是在firefox和safari會報錯

firefox錯誤訊息是 SyntaxError: invalid regexp group
safari錯誤訊息是 Invalid regular expression: unrecognized character after (?

查過可能是js不支援?<=的問題

怎么改寫這段比較好呢?

或是各位有更好的切割字串寫法也可以提出

回答
編輯回答
玩控

如果確定是以http,https開頭的,可以使用

str.match(/(http|https):\/\/.+?\.(jpg|JPG|png|PNG)/)

1、使用split切割

function a(str){
    var arr = []
    var strs = str.split(/(\.jpg|\.JPG|\.png|\.PNG)/)
    strs.map((item,i)=>{
        if(i%2 == 0){
            arr.push(strs[i-1]+item)
        }
    })
    return arr
}
console.log(a(str))

2.使用replace

function b(str){
    var index = 0,
        arr = [];
    str.replace(/(\.jpg|\.JPG|\.png|\.PNG)/g,(match, p1, i, p3, offset, string)=>{
        arr.push(str.substring(index,i+match.length));
        index = i+match.length;
    })
    return arr
}
console.log(b(str))
2017年1月4日 00:54