3 回答

TA貢獻1801條經驗 獲得超8個贊
我認為 Firefox 還不支持lookbehinds。相反,您可以使用捕獲組進行拆分以保留定界符,匹配組內不是正斜杠的任何前面的字符,然后過濾以刪除空字符串。例如:
let s = 'a/b/c'.split(/([^/]*\/)/).filter(x => x);
console.log(s);
// ["a/", "b/", "c"]
let s = 'a//b'.split(/([^/]*\/)/).filter(x => x);
console.log(s);
// ["a/", "/", "b"]

TA貢獻1827條經驗 獲得超9個贊
我不知道為什么那個東西在其他瀏覽器中不起作用。但是您可以使用以下技巧replace:
var text = 'a/b/c';
var splitText = text
.replace(new RegExp(/\//, 'g'), '/ ') // here you are replacing your leading slash `/` with slash + another symbol e.g. `/ `(space is added)
.split(' ') // to further split with added symbol as far as they are removed when splitting.
console.log(splitText) // ["a/", "b/", "c"]
通過這種方式,您可以避免使用在 FireFox、Edge 等中不起作用的復雜正則表達式。
添加回答
舉報